42👍
✅
You have to specify that it’s a nested relationships:
class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = ('name',)
class UserSerializer(serializers.ModelSerializer):
groups = GroupSerializer(many=True)
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff', 'groups',)
Check documentation for more information : Nested relationships
6👍
Something like this should work.
from django.contrib.auth.models import Group
class UserSerializer(serializers.ModelSerializer):
groups = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field='name',
)
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff', 'groups',)
- Django-templates: Why doesn't {% if "string"|length > 10 %} work at all?
- Django: serving ADMIN media files
- When to use NullBooleanField in Django
- Unable to install MySQL-python
- How to setup SysLogHandler with Django 1.3 logging dictionary configuration
Source:stackexchange.com