动态更新ModelForm的Meta类模型字段

发布于 2024-08-21 19:39:59 字数 603 浏览 3 评论 0原文

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 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)

No matter what is passed into model_cls, model is always ignored.

Meta.exclude is set properly, how can I dynamically update the Meta.model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜嗑 2024-08-28 19:39:59
def get_form(model_class):
    class DynamoForm(forms.ModelForm):
        class Meta:
            model = model_class

    return DynamoForm


form_set = get_form(ActualModel)(request.POST)

这就是你想要的吗?

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

    return DynamoForm


form_set = get_form(ActualModel)(request.POST)

is that what you want ?

久夏青 2024-08-28 19:39:59

这是因为您的 merchant.__class__ 也是 django.db.models.base.ModelBase。如果您只是尝试打印商家,那么您将获得正确的类路径(我得到app.models.TestModel),但class是BaseModel。

我相信这是因为元类的使用。元类可能将对象的类设置为 ModelBase。 Django 广泛使用这个 python 功能,这可能会导致一些奇怪的行为(这对那些不完全理解它的人来说意味着奇怪,就像我一样;-),而不是说它不起作用)。我会尝试看一下 django 代码 - 你也应该:-)

不管怎样,你在表单中得到了你想要的东西,它应该工作得很好:-)

DAMMIT

当然我知道,为什么会发生这种情况。你知道,你正在要求一个班级的班级。 form.Meta.model 内部是一个类,即您传递到表单中的类。这就是您获得 ModelBase 的原因 - 您获得了元类。

关于元类的内容值得一读,因为它们很酷。虽然它们也有点复杂 ;-) 在这里可以阅读有关它们的内容。

That's because your merchant.__class__ is also django.db.models.base.ModelBase. If you just try to print merchant, then you will get proper class path (I get app.models.TestModel), but class is BaseModel.

I believe that's because of metaclasses use. Probably the metaclass sets the class of an object to ModelBase. Django extensively use this python feature and this may cause some strange behaviour (which means strange for people, that don't exactly get it, like me ;-), not that it doesn't work). I'll try to take a look at django code - you should too :-)

Anyway, you get what you want inside your form and it should work just fine :-)

DAMMIT

Of course I know, why this is happening. You are asking for the class of a class, you know. Inside form.Meta.model is a class, the one you have passed into the form. This is why you get ModelBase - you get the metaclass.

It's worth reading about metaclasses, because they are cool. Though they are a bit complicated too ;-) Here you can read about them.

夜访吸血鬼 2024-08-28 19:39:59

尝试这样使用:

merchant_form = SiteAdminForm(merchant.model, instance=merchant)

一个建议是使用 Python shell(在 django 中为 ./manage.py shell)通过内置 dir() 函数检查对象的属性和方法。

Try to use like this:

merchant_form = SiteAdminForm(merchant.model, instance=merchant)

One suggestion is to use the Python shell (in django case, ./manage.py shell) to inspect attributes and methods of an object with the built-in dir() function.

你另情深 2024-08-28 19:39:59

我是个白痴,睡在上面,再看一遍,几乎立刻就明白了!!!!

return MerchantAdminForm() 应该是:return MerchantAdminForm(*args, **kwargs)

I'm an idiot, slept on it, looked at it again and figured it out almost immediately!!!!

return MerchantAdminForm() should have been: return MerchantAdminForm(*args, **kwargs)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文