简体   繁体   English

一个模型如何具有多个具有与值相同类型的模型的键?

[英]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.

相关问题 如何将具有多个文本列表值的python字典拆分为具有相同值的键的单独字典? - How can I split a python dictionary with multiple text-list values to have separate dictionaries of keys that have the same values? 如何根据值对字典进行排序,如果多个键具有相同的值,则按键排序 - How to sort dictionary based on values and if multiple keys have same values then sort by keys 如何将 2 个列表组合成一个字典,其中键可以有多个值? - How to combine 2 lists into a dictionary, where keys can have multiple values? 如何在python中合并两个具有相同键和数组类型值的嵌套字典? - How to merge two nested dictionaries in python, which have same keys and have array type values? XML 使用具有多个键值的相同标签来 dict Python - XML to dict Python with same tags that have multiple keys values 如果值相同,如何合并和切换键值? - How do you merge and switch keys with values if they have same values? Django:当父模型有两个外键来自同一模型时,如何定义模型? - Django: How to define the models when parent model has two foreign keys come from one same model? Python:具有相同值(可变类型)的多个键。 更改这些值之一也会更新其他值。 如何避免这种情况? - Python: Multiple keys with same values (mutable type). Changing one of these values updates the other values too. How do I avoid this? 如何在 JSON 文件中更改多个键 - How can I change multiple keys in a JSON file that has multiple dictionaries which incorporate the same keys but different values with Python 为什么 Python dict 可以有多个具有相同散列的键? - Why can a Python dict have multiple keys with the same hash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM