如何访问继承类的内部类并对其进行修改?

时间:2022-11-28 09:12:35

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.

我希望扩展这个类(一个ModelForm),所以我有一个B类。

Class B will look like this:

B类看起来像这样:

class B(ProductVariantForm_PRE):

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

如何访问B类中的内部类“Meta”并修改排除字段?

Thanks!

1 个解决方案

#1


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

在这里看一下Django文档的模型继承。从该页面:

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:

当创建抽象基类时,Django使您在基类中声明的任何Meta内部类可用作属性。如果子类没有声明它自己的Meta类,它将继承父类的Meta。如果孩子想要扩展父类的Meta类,它可以将其子类化。例如:

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

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

#1


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

在这里看一下Django文档的模型继承。从该页面:

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:

当创建抽象基类时,Django使您在基类中声明的任何Meta内部类可用作属性。如果子类没有声明它自己的Meta类,它将继承父类的Meta。如果孩子想要扩展父类的Meta类,它可以将其子类化。例如:

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

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