简体   繁体   中英

How do I access an inherited class's inner class and modify it?

So I have a class, specifically, this:

class ProductVariantForm_PRE(ModelForm):
    class Meta:
        model = ProductVariant
        exclude = ("productowner","status")
    def clean_meta(self):

        if len(self.cleaned_data['meta']) == 0:
            raise forms.ValidationError(_(u'You have to select at least 1 meta attribute.'))

        for m in self.cleaned_data['meta']:
            for n in self.cleaned_data['meta']:
                if m != n:
                    if m.name == n.name:
                        raise forms.ValidationError(_(u'You can only select 1 meta data of each type. IE: You cannot select 2 COLOR DATA (Red and Blue). You can however select 2 different data such as Shape and Size.'))
        return self.cleaned_data['meta']

I wish to extend this class (a ModelForm), and so I have a class B.

Class B will look like this:

class B(ProductVariantForm_PRE):

How can I access the inner class "Meta" in class B and modify the exclude field?

Thanks!

Take a look at the Django documentation for model inheritance here . From that page:

When an abstract base class is created, Django makes any Meta inner class you declared in the base class available as an attribute. If a child class does not declare its own Meta class, it will inherit the parent's Meta. If the child wants to extend the parent's Meta class, it can subclass it. For example:

class CommonInfo(models.Model):
    ...
    class Meta:
        abstract = True
        ordering = ['name']

class Student(CommonInfo):
    ...
    class Meta(CommonInfo.Meta):
        db_table = 'student_info'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM