101
votes

How do I serialize a many-to-many field into list of something, and return them through rest framework? In my example below, I try to return the post together with a list of tags associated with it.

models.py

class post(models.Model):
    tag = models.ManyToManyField(Tag)
    text = models.CharField(max_length=100)

serializers.py

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ("text", "tag"??)

views.py

class PostViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
7
Using help from @Brian I manage to list the items in this form: "tags": [{"name": "tag1"}]. I would like to simplify it to list, is it possible: "tags": ["tag1", "tag2",...]kengcc
use ` tags = serializers.SlugRelatedField(many=True,read_only=True, slug_field='title', //tag's fireld you want to show allow_null=True)` in PostSerializersM. Dhaouadi

7 Answers

126
votes

You will need a TagSerializer, whose class Meta has model = Tag. After TagSerializer is created, modify the PostSerializer with many=True for a ManyToManyField relation:

class PostSerializer(serializers.ModelSerializer):
    tag = TagSerializer(read_only=True, many=True)

    class Meta:
        model = Post
        fields = ('tag', 'text',)

Answer is for DRF 3

31
votes

This is what I did, let´s suppose a Book can have more than one author and an Author can have more than one book: On Model:

class Author(models.Model):
    name = models.CharField(max_length=100, default="")
    last_name = models.IntegerField(default=0)

class Book(models.Model):
    authors = models.ManyToManyField(Author, related_name="book_list", blank=True)
    name = models.CharField(max_length=100, default="")
    published = models.BooleanField(default=True)

On Serializers:

class BookSerializer(serializers.ModelSerializer):
    authors = serializers.PrimaryKeyRelatedField(queryset=Author.objects.all(), many=True)

    class Meta:
        model = Book
        fields = ('id', 'name', 'published', 'authors')


class AuthorSerializer(serializers.ModelSerializer):
    book_list = BookSerializer(many=True, read_only=True)

    class Meta:
        model = Author
        fields = ('id', 'name', 'last_name', 'book_list')
9
votes

Adding to @Brian's answer "tags": [{"name": "tag1"}] can be simplified to "tags": ["tag1", "tag2",...] in this way:

class PostSerializer(serializers.ModelSerializer):
    tag = TagSerializer(read_only=True, many=True)

    class Meta:
        ...

class TagSerializer(serializers.RelatedField):

     def to_representation(self, value):
         return value.name

     class Meta:
        model = Tag

More info here: https://www.django-rest-framework.org/api-guide/relations/#custom-relational-fields

7
votes

The default ModelSerializer uses primary keys for relationships. However, you can easily generate nested representations using the Meta depth attribute:

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ("text", "tag")
        depth = 1 

As mentioned in the documentation :

The depth option should be set to an integer value that indicates the depth of relationships that should be traversed before reverting to a flat representation.

4
votes

This works for me.

tag = TagSerializer(source="tag", read_only=True, many=True)
4
votes

Django 2.0

For many to many field, if you want specific one:

class QuestionSerializer(serializers.ModelSerializer):

    topics_list = serializers.SerializerMethodField()

    def get_topics_list(self, instance):
        names = []
        a = instance.topics.get_queryset()
        for i in a:
            names.append(i.desc)
        return names
    class Meta:
        model = Question
        fields = ('topics_list',)
2
votes

In the serializer on init method you can pass the queryset to the field and rest_framework valide the ids on that queryset

1) first extend your serializer from serializers.ModelSerializer

class YourSerializer(serializers.ModelSerializer):

2) include the field on the meta class

class YourSerializer(serializers.ModelSerializer):
  class Meta:
        fields = (..., 'your_field',)

3) in the init method:

def __init__(self, *args, **kwargs):
    super(YourSerializer, self).__init__(*args, **kwargs)
    self.fields['your_field].queryset = <the queryset of your field>

You can limit the queryset for that field under any argument using filter or exclude like normally you do. In case that you want include all just use .objects.all()