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
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.