I am serializing the built-in django Group model and would like to add a field to the serializer that counts the number of users in the group. I am currently using the following serializer:
class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = ('id', 'name', 'user_set')
This returns the group ID and name and an array of users (user IDs) in the group:
{
"id": 3,
"name": "Test1",
"user_set": [
9
]
}
What I would like instead as output is something like:
{
"id": 3,
"name": "Test1",
"user_count": 1
}
Any help would be appreciated. Thanks.