动态更新ModelForm的Meta类模型字段
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是你想要的吗?
is that what you want ?
这是因为您的
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 alsodjango.db.models.base.ModelBase
. If you just try to print merchant, then you will get proper class path (I getapp.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.
尝试这样使用:
一个建议是使用 Python shell(在 django 中为 ./manage.py shell)通过内置 dir() 函数检查对象的属性和方法。
Try to use like this:
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.
我是个白痴,睡在上面,再看一遍,几乎立刻就明白了!!!!
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)