简体   繁体   English

通用关系不能反映在Django的迁移中吗?

[英]Generic Relation not reflecting in migrations in django?

I'm trying to add a GenericRelation field to the Notification model to my "Bond" model. 我正在尝试将“通知”模型的GenericRelation字段添加到“债券”模型。 The issue is that whenever I run makemigrations, this field does not get acknowledged. 问题是,每当我运行makemigrations时,此字段都不会得到确认。 What could be the issue? 可能是什么问题?

Error: 错误:

django.core.exceptions.FieldError: Field 'content_object' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. django.core.exceptions.FieldError:字段“ content_object”不会生成自动反向关系,因此不能用于反向查询。 If it is a GenericForeignKey, consider adding a GenericRelation. 如果它是GenericForeignKey,请考虑添加GenericRelation。

class Bond(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name="follower")
    bond_created = models.DateTimeField(default=now)
    notifications = GenericRelation(Notification)
class Notification(models.Model):

    #337, 777, 765, 843, 124
    notification_type = models.PositiveIntegerField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

Try to place the Notification model above the Bond model in models.py. 尝试将通知模型放置在models.py中的Bond模型上方。 I can't say that it will solve the problem,but you can try. 我不能说它将解决问题,但是您可以尝试。 And if this also not work,try to first delete all new fields then do makemigrations migrate then again add those new fields and do makemigrations and migrate. 如果这还是行不通,请尝试先删除所有新字段,然后进行makemigrations迁移,然后再次添加这些新字段,然后进行makemigrations和迁移。 Hope this will work. 希望这会起作用。

尝试为相关模型添加唯一的related_query_name ,请参阅。

notifications = GenericRelation(Notification, related_query_name='bond')

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

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