Got attribute error when trying creating model
I got this error message
Got AttributeError when attempting to get a value for field id
on serializer IngredientSerializer
.
The serializer field might be named incorrectly and not match any attribute or key on the Ingredient
instance.
Original exception text was: 'Ingredient' object has no attribute 'ingredient'.
when trying to save model in DRF.
My serializers:
from django.db import transaction
from rest_framework import serializers
from .models import Recipe, Ingredient, RecipeIngredient, Tag
class IngredientSerializer(serializers.ModelSerializer):
id = serializers.PrimaryKeyRelatedField(queryset=Ingredient.objects.all, source="ingredient")
amount = serializers.IntegerField()
class Meta:
model = RecipeIngredient
fields = ["id", "amount"]
class CreateRecipeSerializer(serializers.ModelSerializer):
ingredients = IngredientSerializer(many=True)
tags=serializers.PrimaryKeyRelatedField(...)
image = serializers.ImageField(required=False)
name = serializers.CharField(source="recipe_name")
class Meta:
model = Recipe
fields = ["name", "image", "text", "cooking_time", "ingredients", "tags"]
def create(self, validated_data):
ingredients_data = validated_data.pop("ingredients")
tags_data = validated_data.pop("tags")
with transaction.atomic():
recipe = Recipe.objects.create(**validated_data)
recipe.tags.set(tags_data)
recipe_ingredients = [
RecipeIngredient(
recipe=recipe,
ingredient=ingredient_data["ingredient"],
amount=ingredient_data["amount"],
)
for ingredient_data in ingredients_data]
RecipeIngredient.objects.bulk_create(recipe_ingredients)
return recipe
And my models here
class Ingredient(models.Model):
ingredient_name = models.CharField(max_length=MAX_LENGTH_200)
measurement_unit = models.CharField(max_length=MAX_LENGTH_200)
class Recipe(models.Model):
ingredients = models.ManyToManyField(Ingredient, through="RecipeIngredient", related_name="recipes")
tags = models.ManyToManyField(Tag, related_name="recipes")
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="recipes")
recipe_name = models.CharField(max_length=MAX_LENGTH_200)
image = models.ImageField(upload_to="recipes/images/")
text = models.TextField()
cooking_time = models.PositiveIntegerField(validators= [MinValueValidator(1),MaxValueValidator(50000)])
class RecipeIngredient(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE,
related_name="recipe_ingredients")
ingredient = models.ForeignKey(Ingredient,
on_delete=models.CASCADE, related_name="ingredients")
amount = models.PositiveIntegerField()
I can't understand what the problem might be, I tried to write both on the usual serializer and as above with the help of ModelSerializer, but I get the same error.
Using django 3.2.25 if that matters in this case. I would be grateful for any help
You use recipe_ingredients
as source for the ingredients
, not , so:ingredients
class CreateRecipeSerializer(serializers.ModelSerializer):
ingredients = IngredientSerializer(many=True, source='recipe_ingredients')
tags = serializers.PrimaryKeyRelatedField(
queryset=Tag.objects.all(), many=True
)
image = serializers.ImageField(required=False)
name = serializers.CharField(source='recipe_name')
class Meta:
model = Recipe
fields = ['name', 'image', 'text', 'cooking_time', 'ingredients', 'tags']