【问题标题】:Django API ManyToMany on existing databse not working现有数据库上的 Django API ManyToMany 不起作用
【发布时间】:2021-02-23 09:18:42
【问题描述】:

目前我尝试从我的 API 获取食谱。我有一个包含两个表的数据库,其中一个包含食谱及其 ID,但没有成分,另一个表包含成分和食谱 ID。现在我找不到API“组合”这些的方法。也许是因为我在我的成分模型中添加了相关名称的配方 ID,但我必须这样做,否则会发生此错误:

ERRORS:
recipes.Ingredients.recipeid: (fields.E303) Reverse query name for 'Ingredients.recipeid' clashes with field name 'Recipe.ingredients'.
        HINT: Rename field 'Recipe.ingredients', or add/change a related_name argument to the definition for field 'Ingredients.recipeid'.

型号

from django.db import models

class Ingredients(models.Model):
    ingredientid = models.AutoField(db_column='IngredientID', primary_key=True, blank=True)
    recipeid = models.ForeignKey('Recipe', models.DO_NOTHING, db_column='recipeid', blank=True, null=True, related_name='+')
    amount = models.CharField(blank=True, null=True, max_length=100)
    unit = models.CharField(blank=True, null=True, max_length=100)
    unit2 = models.CharField(blank=True, null=True, max_length=100)
    ingredient = models.CharField(db_column='Ingredient', blank=True, null=True, max_length=255)
    
    class Meta:
        managed = False
        db_table = 'Ingredients'  

class Recipe(models.Model):
    recipeid = models.AutoField(db_column='RecipeID', primary_key=True, blank=True)  # Field name made lowercase.
    title = models.CharField(db_column='Title', blank=True, null=True, max_length=255)  # Field name made lowercase.
    preperation = models.TextField(db_column='Preperation', blank=True, null=True)  # Field name made lowercase.
    images = models.CharField(db_column='Images', blank=True, null=True, max_length=255)  # Field name made lowercase.
    #ingredients = models.ManyToManyField(Ingredients) 
    ingredients = models.ManyToManyField(Ingredients, related_name='recipes')
    class Meta:
        managed = True
        db_table = 'Recipes'

如果没有问题,则必须在序列化程序或视图中。

序列化器

class IngredientsSerializer(serializers.ModelSerializer):
   # ingredients = serializers.CharField(source='ingredients__ingredients')

    class Meta:
        model = Ingredients
        fields = ['ingredient','recipeid']

class FullRecipeSerializer(serializers.ModelSerializer):

    ingredients = IngredientsSerializer(many=True)

    class Meta:
        model = Recipe
        fields = ['title','ingredients']

查看

class FullRecipesView(generics.ListCreateAPIView):
    serializer_class = FullRecipeSerializer
    permission_classes = [
        permissions.AllowAny
    ]
    queryset = Recipe.objects.all()

This is at the moment my output 但我想要例如id 为 0 的配方以及所有配方 id 为 0 的成分。 我真的希望你能帮助我。非常感谢!

【问题讨论】:

    标签: python python-3.x django django-models django-rest-framework


    【解决方案1】:

    ingredients 重命名为FullRecipeSerializer 中的其他名称。它与Recipe 模型中的ingredients 冲突。这应该可以解决您的问题。例如

    class FullRecipeSerializer(serializers.ModelSerializer):
    
        ingredients_recipe = IngredientsSerializer(many=True, source= 'ingredientid')
    
        class Meta:
            model = Recipe
            fields = ['title','ingredients_recipe']
    

    【讨论】:

    • 你的意思是两个?一个在字段中,其他成分可变?
    • 然后出现此错误:“在序列化程序 FullRecipeSerializer 上尝试获取字段 ingredients_recipe 的值时出现 AttributeError。序列化程序字段可能命名不正确,并且与Recipe 实例。原始异常文本为:'Recipe' 对象没有属性 'ingredients_recipe'。"
    • 将源添加到ingredients_recipe
    • 现在'int'对象是不可迭代的,可能是因为recipe id是一个int?
    • int 问题是 many=True 但我现在得到的是 imgur.com/a/9vaoqRu
    猜你喜欢
    • 2015-10-26
    • 2016-01-02
    • 2018-02-09
    • 1970-01-01
    • 2021-11-25
    • 2017-12-14
    • 2020-09-29
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多