gpt4 book ai didi

django - django 中的抽象形式或形式组合?

转载 作者:行者123 更新时间:2023-12-04 02:44:44 24 4
gpt4 key购买 nike

我有一个表格:

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

我现在不知 Prop 体怎么做。我尝试使用 mixin,但在字段初始化方面存在一些问题,应该兼顾内部方法的调用。我可以使用抽象模型对模型做类似的事情 docs .有没有办法用表格来做这种组合?

最佳答案

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

class RideForm(RideConditionPartForm):

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

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

关于django - django 中的抽象形式或形式组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19052648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com