2👍
You can see that instead using to
related name, you need to use related_to
, which is pointing to an actual user object (to
is pointing to Relationship
object instead). That’s why you get this exception.
With a little help of the following tip: https://stackoverflow.com/a/24216503/1566605,
you can display nested users:
class RecursiveField(serializers.Serializer):
def to_native(self, value):
return self.parent.to_native(value)
class UserSerializer(serializers.ModelSerializer):
related_to = RecursiveField(many=True)
class Meta:
model = User
fields = ('id', 'username', 'first_name', 'last_name', 'related_to')
read_only_field = 'username'
Source:stackexchange.com