I need a nested serializer in django. I have the following serializer that works:
class LocationSerializer(serializers.ModelSerializer):
coordinate = serializers.SerializerMethodField()
message = serializers.SerializerMethodField()
def get_message(self, instance: models.Location):
if len(instance.message) > 1:
return instance.message
return None
def get_coordinate(self, instance: models.Location):
if instance.point:
return {"latitude": instance.point.y, "longitude": instance.point.x}
class Meta:
model = models.Location
fields = ('id', 'street', 'zip', 'phone', 'coordinate', 'message')
The json this serializer produces needs to be wrapped in a a json object like this:
{
"locations": //here comes the serialized data
}
I tried it the following way with another serializer that has the serializer above in a field:
class LocationResponseSerializer(serializers.Serializer):
locations = LocationSerializer(many=True)
But when I trie to use this serializer I always get the following error:
The serializer field might be named incorrectly and not match any attribute or key on the `Location` instance.
Original exception text was: 'Location' object has no attribute 'locations'.
What am I doing wrong? Just wrapping it in a response object works, but is not a solution because it looks like this is not supported by the swagger frameworks that work with Django.
Thanks for the help!
Edit: This is the list method of my view:
def list(self, request, *args, **kwargs):
# update_compartments()
queryset = Location.objects.filter(hidden=False).all()
serializer = LocationResponseSerializer(queryset, many=True)
return Response(serializer.data)