我需要“覆盖”一些基类的嵌套类成员,同时保持其余部分不变。
这就是我所做的:
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
事实上,我希望InternApplicationForm.Meta
完全像InternGenericForm.Meta
,除了它的exclude
元组应该再包含一项。
在 Python 中执行此操作的更漂亮的方法是什么?
我希望我不必编写像 model = InternGenericForm.Meta.model
这样也容易出错的样板代码。
最佳答案
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
关于python - 在 Python 中覆盖嵌套类成员的更好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7717089/