使用内容类型 (object_pk) 创建通用关系时出错
我正在努力使用 django 的 ContentType 框架为我的模型创建一些通用关系;在查看了 django 开发人员在 django.contrib.comments.models 上的做法之后,我想我会模仿他们的方法/约定:
来自 django.contrib.comments.models,第 21 行):
content_type = models.ForeignKey(ContentType,
verbose_name='content type',
related_name="content_type_set_for_%(class)s")
object_pk = models.TextField('object ID')
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
这是取自他们的来源,当然,他们的源对我有用(我对 object_pk 的存储很好(实际上是整数)进行了评论;但是,我在创建表时的 syncdb
过程中收到一个错误:
_mysql_exceptions.OperationalError: (1170, "BLOB/TEXT column 'object_pk' used in key specification without a key length")
任何想法为什么他们可以做到这一点,我可以 环顾四周
后,我注意到文档 实际上声明:
为您的模型提供一个字段,该字段可以存储您将关联的模型的主键值。 (对于大多数模型,这意味着 IntegerField 或 PositiveIntegerField。)
此字段的类型必须与通用关系中涉及的模型的主键类型相同。例如,如果您使用 IntegerField,则无法与使用 CharField 作为主键的模型形成通用关系。
但为什么他们能做到,而我却不能?!
谢谢。
PS:我什至尝试使用这三个字段创建一个 AbstractBaseModel,使其 abstract=True
并使用它(如果与它有关)......同样的错误。
I am working to use django's ContentType framework to create some generic relations for a my models; after looking at how the django developers do it at django.contrib.comments.models
I thought I would imitate their approach/conventions:
from django.contrib.comments.models, line 21):
content_type = models.ForeignKey(ContentType,
verbose_name='content type',
related_name="content_type_set_for_%(class)s")
object_pk = models.TextField('object ID')
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
That's taken from their source and, of course, their source works for me (I have comments with object_pk's stored just fine (integers, actually); however, I get an error during syncdb
on table creation that ends:
_mysql_exceptions.OperationalError: (1170, "BLOB/TEXT column 'object_pk' used in key specification without a key length")
Any ideas why they can do it and I can't ?
After looking around, I noticed that the docs actually state:
Give your model a field that can store a primary-key value from the models you'll be relating to. (For most models, this means an IntegerField or PositiveIntegerField.)
This field must be of the same type as the primary key of the models that will be involved in the generic relation. For example, if you use IntegerField, you won't be able to form a generic relation with a model that uses a CharField as a primary key.
But why can they do it and not me ?!
Thanks.
PS: I even tried creating an AbstractBaseModel with these three fields, making it abstract=True
and using that (in case that had something to do with it) ... same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我输入了那个很长的问题后,我查看了 mysql,意识到错误源于:
显然,我不能同时拥有这两种方式。这让我心碎。我必须提出一个新问题,询问是否最好将我的
object_pk
选项保持打开状态(假设我使用文本字段作为主键?),还是最好强制执行unique_togetherness
代码>...After I typed out that really long question I looked at the mysql and realized that the error was stemming from:
Apparently, I can't have it both ways. Which leaves me torn. I'll have to open a new question about whether it is better to leave my
object_pk
options open (suppose I use a textfield as a primary key?) or better to enforce theunique_togetherness
...