ahmet yılmaz234
ahmet yılmaz234

Reputation: 87

Pass objects to a serializer in Django Rest Framework

Here I have 2 objects recipe objects in my Recipe model. I am trying to understand what happens when I pass the recipe objects to my serializer like this. What system does to that objects? And what it returns?

recipes = Recipe.objects.all().order_by('-id')
serializer = RecipeSerializer(recipes, many=True)

And here is serializer:

class RecipeSerializer(serializers.ModelSerializer):


    class Meta:
        model = Recipe
        fields = '__all__'

Upvotes: 2

Views: 1418

Answers (1)

Noureddine
Noureddine

Reputation: 36

serializer = RecipeSerializer(recipes, many=True)

This means that you will take the data from the RecipeSerializer, not from the model recipe; in your case, that will be the same except the form (the serializer returns data as OrderedDict), but in case that the serializer have more data than the model, it's going to be more useful to call the serializer.data.

Upvotes: 2

Related Questions