def SiteAdminForm(model_cls, *args, **kwargs):
    class MerchantAdminForm(forms.ModelForm):
        class Meta:
            exclude = ('external_links', 'published', 'logo','image_zip_file',)
            model = model_cls

        def __init__(self, *args, **kwargs):
            super(MerchantAdminForm, self).__init__(*args, **kwargs)

    return MerchantAdminForm()

# In use...
merchant_form = SiteAdminForm(merchant.__class__, instance=merchant)


无论将什么传递给model_cls,始终都会忽略模型。

Meta.exclude的设置正确,如何动态更新Meta.model

最佳答案

def get_form(model_class):
    class DynamoForm(forms.ModelForm):
        class Meta:
            model = model_class

    return DynamoForm


form_set = get_form(ActualModel)(request.POST)


那是你要的吗 ?

关于python - 动态更新ModelForm的元类模型字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2239096/

10-12 23:05