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

2

You use recipe_ingredients as source for the ingredients, not ingredients, so:

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']

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật