在 Django 中,使用 GenericForeignKey 时是否需要将 db_index 添加到 object_id ?

eug*_*ene 4 django generic-foreign-key

Django 的示例代码位于https://docs.djangoproject.com/es/1.9/ref/contrib/contenttypes/

content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
Run Code Online (Sandbox Code Playgroud)

显示 object_id 不带db_index=True

虽然object_id可能会用于连接操作。

db_index=True因为一些关于 GFK 的隐藏代码而被假设吗?或者我应该在需要时包含它?

Ale*_*zov 6

是的,您应该object_id自己使该字段可索引。从 1.9 开始,Django对此没有任何魔法。