簡體   English   中英

通用關系不能反映在Django的遷移中嗎?

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

我正在嘗試將“通知”模型的GenericRelation字段添加到“債券”模型。 問題是,每當我運行makemigrations時,此字段都不會得到確認。 可能是什么問題?

錯誤:

django.core.exceptions.FieldError:字段“ content_object”不會生成自動反向關系,因此不能用於反向查詢。 如果它是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')

嘗試將通知模型放置在models.py中的Bond模型上方。 我不能說它將解決問題,但是您可以嘗試。 如果這還是行不通,請嘗試先刪除所有新字段,然后進行makemigrations遷移,然后再次添加這些新字段,然后進行makemigrations和遷移。 希望這會起作用。

嘗試為相關模型添加唯一的related_query_name ,請參閱。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM