class PersonSerializer(serializers.ModelSerializer):
children = serializers.SerializerMethodField()
class Meta:
model = Person
fields = ['id', 'first_name', 'last_name', 'mother', 'father', 'children']
def get_children(self, obj):
# Get all children of the demo
children_as_father = obj.children_as_father.values_list('id', flat=True)
children_as_mother = obj.children_as_mother.values_list('id', flat=True)
return list(children_as_father) + list(children_as_mother)
https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield