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(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 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.
1 Answer