使用内容类型 (object_pk) 创建通用关系时出错

发布于 2024-08-08 13:32:12 字数 1359 浏览 3 评论 0原文

我正在努力使用 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 技术交流群。

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

发布评论

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

评论(1

尾戒 2024-08-15 13:32:12

在我输入了那个很长的问题后,我查看了 mysql,意识到错误源于:

class Meta:
    unique_together = (("content_type", "object_pk"),)

显然,我不能同时拥有这两种方式。这让我心碎。我必须提出一个新问题,询问是否最好将我的 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:

class Meta:
    unique_together = (("content_type", "object_pk"),)

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 the unique_togetherness...

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