我有一张表格:
class RideForm(forms.Form):
# a lot of field
# fields accorded to geography
# fields accorded to condition
def clean(self, *args, **kwargs):
# clean of all fields
我希望像这样拆分它(它只是一个概念,我只是为了说明这个想法而写这个)。 :
class RideGeographyPartForm(...):
# fields accorded to geography
def clean(self, *args, **kwargs):
# clean only geography group of field
class RideConditionPartForm(...):
# fields accorded to condition
def clean(self, *args, **kwargs):
# clean only geography group of field
class RideForm(RideGeographyPartForm, RideConditionPartForm, forms.Form):
def clean(self, *args, **kwargs):
# this should internaly call all clean logic from
# RideGeographyPartForm, RideConditionPartForm
我现在不知道究竟是怎么做到的。我尝试使用mixin但是在字段初始化方面存在一些问题,并且应该调用内部方法。 我可以使用抽象模型docs对模型进行类似的操作。有没有办法做这种组合但有表格?
答案 0 :(得分:3)
多重继承不会做你想要的,如果它们都实现了clean(),只会调用第一个mixin clean()。简单的多态性怎么样?
class RideForm(RideConditionPartForm):
def clean():
super clean()
#other stuff
class RideConditionPartForm(RideGeographyPartForm):
def clean():
super clean()
#other stuff