<p>我的问题是python/django的混合。我有一个表单模型可以显示一些字段。基于此模型的某些参数,发送到创建此对象的元类的数据应该有所不同。但是当我在Meta的主体中时,如何到达这个参数呢?我是否应该使用一些全局变量而不是对象参数(因为引入它只是为了临时存储值)?在</p>
<pre><code>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
</code></pre>