[英]How can a model have multiple keys with the same type of models as values?
I'm making an application and I should make a Model
which have 2 keys that saves models of the same type. 我在做一个应用程序,我应该做一个Model
,其有2个按键,节省了相同类型的模型。
It's not easy to express in English so I will upload the image of the situation. 用英语表达不容易,所以我将上传情况图片。
In Food Pair Model
( or we can call it table I think ) 在Food Pair Model
(或者我可以称其为表)
I want to refer Food Model
But I wasn't able to use ForeignKey or ManyToManyField . 我想引用Food Model
但无法使用ForeignKey或ManyToManyField 。
ERRORS:
food_test.FoodQuestion.left_food: (fields.E304) Reverse accessor for 'FoodQuestion.left_food' clashes with reverse accessor for 'FoodQuestion.right_food'.
HINT: Add or change a related_name argument to the definition for 'FoodQuestion.left_food' or 'FoodQuestion.right_food'.
food_test.FoodQuestion.right_food: (fields.E304) Reverse accessor for 'FoodQuestion.right_food' clashes with reverse accessor for 'FoodQuestion.left_food'.
HINT: Add or change a related_name argument to the definition for 'FoodQuestion.right_food' or 'FoodQuestion.left_food'.
I don't know what database relation to use in this case and how to make it. 我不知道在这种情况下使用什么数据库关系以及如何创建它。
What can I use for this case? 在这种情况下我可以使用什么?
You must define a unique related_name for each ForeignKey field in your FoodPair model. 您必须为FoodPair模型中的每个ForeignKey字段定义一个唯一的related_name。
class FoodPair(models.Model):
first_food = models.ForeignKey(Food, related_name="first_food")
second_food = models.ForeignKey(Food, related_name="second_food")
what_i_buy = models.ForeignKey(Food, related_name="what_i_buy")
If related_name is not defined Django automatically sets it and when there is multiple ForeignKey fields pointing to the same model the names clash. 如果未定义related_name,则Django会自动对其进行设置,并且当有多个ForeignKey字段指向同一模型时,名称会发生冲突。
When you create a ForeignKey
from one model to another Django will dynamically create a property on the model being referenced that will return a QuerySet
with all objects that have the foreign key to that object 当您从一个模型创建到另一个模型的ForeignKey
,Django会在所引用的模型上动态创建一个属性,该属性将返回QuerySet
以及所有具有该对象外键的对象
For example 例如
class Foo(models.Model):
pass
class Bar(models.Model):
foo = models.ForeignKey(Foo, on_delete=models.CASCADE)
foo = Foo.objects.create()
bar = Bar.objects.create(foo=foo)
foo.bar_set.all() # This will return a queryset containing foo
By default this property will be <model_name_lowercase>_set
. 默认情况下,此属性为<model_name_lowercase>_set
。 In your case because you have 2 foreign keys from one model to the same model Django is trying to create the same property on the Food
model for each foreign key. 在您的情况下,因为从一个模型到同一模型有2个外键,Django尝试在Food
模型上为每个外键创建相同的属性。
To get around this issue you can specify the name of this property using related_name
, if you set this to '+' no reverse relation will be made at all or give them unique names 要解决此问题,您可以使用related_name
来指定此属性的名称,如果将此属性设置为“ +”,则不会建立任何反向关系,也不能给它们指定唯一的名称
class FoodQuestion(models.Model):
left_food = models.ForeignKey(Food, on_delete=models.CASCADE, related_name='+')
left_food = models.ForeignKey(Food, on_delete=models.CASCADE, related_name='+')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.