2013-09-27 22 views
1

我有一个表格:django中的抽象表单或表单组合?

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。有没有办法做这种组合,但形​​式?

回答

2

多继承不会做你想要的,如果他们都实现了clean(),只会调用第一个mixin clean()。简单多态性如何?

class RideForm(RideConditionPartForm): 

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

class RideConditionPartForm(RideGeographyPartForm): 
    def clean(): 
    super clean() 
    #other stuff 
+0

:+1:我现在使用这个解决方案,但也许有人知道更多的声明方式来做到这一点 – kharandziuk