Answer the question
In order to leave comments, you need to log in
How to correctly display the data in the serializer?
I have the following models :
models.py
class Ingredient(models.Model):
name = models.CharField(max_length=255, unique=True)
def __str__(self):
return self.name
class Tag(models.Model):
name = models.CharField(max_length=255, unique=True)
def __str__(self):
return self.name
class Recipe(models.Model):
title = models.CharField(max_length=255)
photo = models.ImageField(upload_to="recipe_photo/", blank=True)
lvl_skill = models.CharField(max_length=50)
country_kitchen = models.CharField(max_length=255)
category = models.CharField(max_length=255)
step_cooking = models.TextField()
tags = models.ManyToManyField(Tag)
ingredients = models.ManyToManyField(Ingredient, through='Ingredient_count')
def __str__(self):
return self.title
class Meta:
ordering = ('title',)
class Ingredient_count(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, null=True)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE, null=True)
count = models.CharField(max_length=255)
def __str__(self):
return self.ingredient.name
class RecipeSerializer(serializers.ModelSerializer):
class Meta:
model = Recipe
fields = ['title', 'ingredient_count_set']
depth = 1
Answer the question
In order to leave comments, you need to log in
What does right or wrong mean? Explain what you want to get, like this the name should not be displayed:
def __str__(self):
return self.ingredient.name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question