fieldererror试图删除带有通用外键的Django实例



在修复一些错误时,我做了两个测试实例。现在我已经完成了,我想删除这两个测试:

nj.delete()
raise FieldError("Cannot resolve keyword '%s' into field. "
django.core.exceptions.FieldError: Cannot resolve keyword 'content_type' into field. Choices are: awards, career_highlights, content_object_org, content_object_pc, content_type_org, content_type_org_id, content_type_pc, content_type_pc_id, date_updated, daterange, end_date, honors, object_id_org, object_id_pc, org_history_updated, publications, role, significant_event, start_date, title, uniqid, updated_Vitae_bio_and_org_history

这个错误是不是在我要删除的模型上,而是一个中间模型,它也有一个通用外键。Django找不到字段' content_type ',因为没有这样的字段,所以我不知道它为什么要找它。有一个content_type_org和一个content_type_pc。从上下文中,我假设Django需要content_type_org。但是我该如何告诉Django去寻找它呢?我也试着去超类,从那里删除相同的对象,

jn.delete()

,但得到相同的错误。

正如在评论中提到的,如果没有看到您的模型,很难提供帮助。尽管如此,您似乎已经重命名了GenericForeignKey中使用的content_type字段。您需要使用GenericRelation在相关模型上指定重命名的字段,如下所示:

class TaggedItem(models.Model):

content_type_fk = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_primary_key = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type_fk', 'object_primary_key')

class Blog(models.Model):
tags = GenericRelation(
TaggedItem,
content_type_field='content_type_fk',
object_id_field='object_primary_key',
)

详情请参阅文档

最新更新