3

我有一个表格:

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,但在字段初始化方面遇到了一些问题,应该兼顾调用内部方法。我可以使用抽象模型文档对模型做类似的事情。有没有办法做这种组合但有形式?

4

1 回答 1

4

多重继承不会做你想做的事,如果它们都实现了 clean(),那么只会调用第一个 mixin clean()。简单的多态性怎么样?

class RideForm(RideConditionPartForm):

    def clean():
        super clean()
        #other stuff

class RideConditionPartForm(RideGeographyPartForm):
   def clean():
     super clean()
     #other stuff
于 2013-09-27T16:27:13.093 回答