私の問題は、python/django ミックスです。いくつかのフィールドを表示するフォーム モデルがあります。このモデルのいくつかのパラメーターに基づいて、このオブジェクトを作成するメタクラスに送信されるデータは異なるはずです。しかし、 Meta の本体内でこのパラメーターに到達するにはどうすればよいですか? object パラメーターの代わりにグローバル var を使用する必要がありますか (値を一時的に格納するためだけに導入されているため)。
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