gpt4 book ai didi

python - Django 的两个自引用外键

转载 作者:行者123 更新时间:2023-11-28 21:49:59 24 4
gpt4 key购买 nike

<分区>

在 Django 1.7 中编写一个简单的应用程序。是表示链表元素的模型:

class TrainingUserWordSetItem(models.Model):
training_set = models.ForeignKey(TrainingUserWordSet)
user_word = models.ForeignKey(UserWord)
prev_item = models.ForeignKey("TrainingUserWordSetItem", null=True, default=None)
next_item = models.ForeignKey("TrainingUserWordSetItem", null=True, default=None)

迁移时出现错误:

hellodict.TrainingUserWordSetItem.next_item: (fields.E304) Reverse accessor for 'TrainingUserWordSetItem.next_item' clashes with reverse accessor for 'TrainingUserWordSetItem.prev_item'.
HINT: Add or change a related_name argument to the definition for 'TrainingUserWordSetItem.next_item' or 'TrainingUserWordSetItem.prev_item'.
hellodict.TrainingUserWordSetItem.prev_item: (fields.E304) Reverse accessor for 'TrainingUserWordSetItem.prev_item' clashes with reverse accessor for 'TrainingUserWordSetItem.next_item'.
HINT: Add or change a related_name argument to the definition for 'TrainingUserWordSetItem.prev_item' or 'TrainingUserWordSetItem.next_item'.

如何制作两个自引用外键字段?

更新:

帮助使用 related_name='+':

class TrainingUserWordSetItem(models.Model):
training_set = models.ForeignKey(TrainingUserWordSet)
user_word = models.ForeignKey(UserWord)
prev_item = models.ForeignKey("TrainingUserWordSetItem", null=True, default=None)
next_item = models.ForeignKey("TrainingUserWordSetItem", null=True, default=None, related_name='+')

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com