What is the best way to override nested class members in Python?

I need to โ€œoverrideโ€ some of the classes of nested classes of the base class, leaving them intact.
This is what I do:

class InternGenericForm(ModelForm): class Meta: model = Intern exclude = ('last_achievement', 'program',) widgets = { 'name': TextInput(attrs={'placeholder': '  ' }), } class InternApplicationForm(InternGenericForm): 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 be exactly like InternGenericForm.Meta , except that its exclude tuple must contain one more element.

What is a prettier tool for this in Python? I'm sorry that I do not need to write boilerplate code, for example model = InternGenericForm.Meta.model , which is also error prone.

+6
source share
1 answer
 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 
+13
source

Source: https://habr.com/ru/post/898993/


All Articles