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

时间:2009-07-20 19:12:33

标签: python django

所以我有一节课,特别是这个:

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']

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

B类将如下所示:

class B(ProductVariantForm_PRE):

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

谢谢!

1 个答案:

答案 0 :(得分:4)

查看模型继承here的Django文档。从该页面:

  

当抽象基类是   创建,Django使任何Meta内在   您在基类中声明的类   可用作属性。如果是孩子   class不声明自己的Meta   class,它将继承父级的   元。如果孩子想延长   parent的Meta类,它可以是子类   它。例如:

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

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