Django models.Model 类成员未出现在 model_instance._meta.fields 中
我有一个 django.contrib.contenttypes.generic.genericForeignKeyField 作为我的模型的成员,
但是,当我实例化模型然后尝试从对象的 _meta 中获取字段时,它没有出现。
例如:
class A(models.Model):
field2 = models.IntegerField(...)
field1 = generic.genericForeignKeyField()
a = A()
a._meta.fields ---> this does not show field1, but shows field2.
有人可以告诉我为什么吗?
谢谢 !
I have a django.contrib.contenttypes.generic.genericForeignKeyField as a member of my model,
however, it is not appearing when I instantiate the model and then try to get the fields out of the _meta of the object.
e.g:
class A(models.Model):
field2 = models.IntegerField(...)
field1 = generic.genericForeignKeyField()
a = A()
a._meta.fields ---> this does not show field1, but shows field2.
Can someone please tell me why ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有正确设置通用关系。阅读文档:
最后,它一定是这样的:
Your are not setting up the generic relation correctly. Read the documentation:
In the end, it must be something like:
你为什么会期望它呢?这不是一个真正的领域。它是使用模型上的(真实)
content_type
和object_id
字段计算的虚拟字段。不过,您可以在
a._meta.virtual_fields
中看到它。Why would you expect it to? It's not a real field. It's a virtual field that's calculated using the (real)
content_type
andobject_id
fields on the model.You can however see it in
a._meta.virtual_fields
.