I'm attempting to retrieve a filtered list from a MySQL database. The query itself looks fine, but the JSON returned shows this:
[
{
"id": "0038",
"name": "Jane Doe",
"total_hrs_per_week": 6,
"timezone": "America/Los_Angeles"
},
{
"id": "0039",
"name": "John Doe",
"total_hrs_per_week": 10,
"timezone": "America/Los_Angeles"
}
]
when the spec that I need to build to wants this:
{
"people": [
{
"id": "0038",
"name": "Jane Doe",
"total_hrs_per_week": 6,
"timezone": "America/Los_Angeles"
},
{
"id": "0039",
"name": "John Doe",
"total_hrs_per_week": 10,
"timezone": "America/Los_Angeles"
}
]
}
Here's my serializer
class PeopleListSerializer(serializers.ModelSerializer):
id = serializers.CharField(source='id')
name =serializers.CharField(source='name')
total_hrs_per_week = serializers.IntegerField(source='total_hrs_per_week')
timezone = serializers.CharField(source='timezone')
class Meta:
model = models.Person
fields = ('id','name','total_hrs_per_week','timezone')
Any idea how to wrap the returned results in this way?
EDIT:
I tried wrapping this in another serializer like so
class PeopleListWrapperSerializer(serializers.Serializer):
people = PeopleListSerializer(many=True)
class Meta:
fields = ['people']
But this throws the following error:
Got AttributeError when attempting to get a value for field
peopleon serializerPeopleListWrapperSerializer. The serializer field might be named incorrectly and not match any attribute or key on thePersoninstance. Original exception text was: 'Person' object has no attribute 'people'.