我的问题是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
正如丹尼尔提议的那样,我把整个事情转移到
__init__
:你不能在Meta中做任何动态的事情。不是为了这个。在
你为什么不能做这一切?您可以从那里修改
self.fields
。在相关问题 更多 >
编程相关推荐