简体   繁体   English

运行命令进行迁移时,它会在 django 中引发 GenericForeignKey 错误

[英]When running the command to make migrations it throws a GenericForeignKey error in django

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.auth.models import User


class LikedItems(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    content_Type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()

This is the model that I've created and when I run the following command it give an error.这是我创建的 model,当我运行以下命令时出现错误。

python manage.py makemigrations python manage.py makemigrations

ERROR:错误:

ERRORS:
likes.LikedItems.content_object: (contenttypes.E002) The GenericForeignKey content type ref
erences the nonexistent field 'LikedItems.content_type'.

I had the same problem with mine and the solution was so simple but took some reading through the source code for the django.contrib.contenttypes.fields .我遇到了同样的问题,解决方案非常简单,但阅读了django.contrib.contenttypes.fields的源代码。 If you just want to see the fix to your code just go to the last statement in my comment, everything else is just explaining how I came to my solution.如果您只想查看对我评论中最后一条语句的代码 go 的修复,其他一切都只是在解释我是如何找到我的解决方案的。

Under the _check_content_type_field(self) module in the Generic Foreign Key class, you are going to see your error under a FieldDoesNotExist: condition.在通用外键 class 的 _check_content_type_field(self) 模块下,您将在 FieldDoesNotExist: 条件下看到错误。 A snippet of this from the source code is shown below.下面显示了源代码中的一个片段。

try:
        field = self.model._meta.get_field(self.ct_field)
    except FieldDoesNotExist:
        return [
            checks.Error(
                "The GenericForeignKey content type references the non-existent field '%s.%s'." % (
                    self.model._meta.object_name, self.ct_field
                ),
                hint=None,
                obj=self,
                id='contenttypes.E002',
            )
        ]

IF you look a little bit further down you are going to see an else condition with a nested if statment as shown below in the code snippet.如果你往下看一点,你会看到一个带有嵌套 if 语句的 else 条件,如下面的代码片段所示。

else:
        if not isinstance(field, models.ForeignKey):
            return [
                checks.Error(
                    "'%s.%s' is not a ForeignKey." % (
                        self.model._meta.object_name, self.ct_field
                    ),
                    hint=(
                        "GenericForeignKeys must use a ForeignKey to "
                        "'contenttypes.ContentType' as the 'content_type' field."
                    ),
                    obj=self,
                    id='contenttypes.E003',
                )
            ]

"GenericForeignKeys must use a ForeignKey to"'contenttypes.ContentType' as the 'content_type' “GenericForeignKeys 必须使用外键来”'contenttypes.ContentType' 作为'content_type'

Note here that it states that the field name for the ForeignKey must be content_type not "content_Type" or in my case "content-types"请注意,它指出 ForeignKey 的字段名称必须是content_type而不是“content_Type”,或者在我的例子中是“content-types”

Simply put you need to change the field name for your content type in your model from "content-Type" to just "content-type" .简而言之,您需要将 model 中内容类型的字段名称从"content-Type"更改为"content-type"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM