我的问题是 python/django 混合。我有一个将显示一些字段的表单模型。根据此模型的某些参数,发送到创建此对象的元类的数据应该不同。但是,在 Meta 的主体内,我怎样才能达到这个参数呢?我应该使用一些全局变量而不是对象参数(因为它只是为了临时存储值而引入的)?
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
instance = kwargs.get("instance")
self.type = None
try:
type = self.instance.template_id
except:
pass
class Meta:
model = ContentBase
fields = ["title", "slug", "description", "text", "price",]
#here I need to have the value of 'type'
if type != 2:
try:
fields.remove("price")
except:
pass