开发者

What's a better way of overriding nested class members in Python?

I need to “override” some of the base class' nested class members, while keeping the rest intact.

This is what I do:

class InternGenericForm(ModelForm):                
    class Meta:
        model = Intern
        exclude = ('last_achievement', 'program',)
        widgets = {
            'name': TextInput(attrs={'placeholder': 'Имя и фамилия' }),
        }

class InternApplicationForm(Intern开发者_如何学CGenericForm):
    class Meta:
        # Boilerplate code that violates DRY
        model = InternGenericForm.Meta.model
        exclude = ('is_active',) + InternGenericForm.Meta.exclude
        widgets = InternGenericForm.Meta.widgets

In fact, I want InternApplicationForm.Meta to be exactly like InternGenericForm.Meta, except that its exclude tuple should contain one more item.

What is a more beautiful way of doing this in Python?

I wish I didn't have to write boilerplate code like model = InternGenericForm.Meta.model that is also prone to errors.


class InternGenericForm(ModelForm):                
    class Meta:
        model = Intern
        exclude = ('last_achievement', 'program',)
        widgets = {
            'name': TextInput(attrs={'placeholder': 'Имя и фамилия' }),
        }

class InternApplicationForm(InternGenericForm):
    class Meta(InternGenericForm.Meta):
        exclude = ('is_active',) + InternGenericForm.Meta.exclude
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜