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 people on serializer PeopleListWrapperSerializer. The serializer field might be named incorrectly and not match any attribute or key on the Person instance. Original exception text was: 'Person' object has no attribute 'people'.

  • Try without fields = ['people'], it will take that field anyway. Maybe it gets confused there. But it's only a guess. – Karina Klinkevičiūtė May 12 '16 at 9:11
up vote 3 down vote accepted

You can do that by overriding the list() method.

class PeopleListView(ListAPIView):

    def list(self, request, *args, **kwargs):
        # call the original 'list' to get the original response
        response = super(PeopleListView, self).list(request, *args, **kwargs) 

        # customize the response data
        response.data = {"people": response.data} 

        # return response with this custom representation
        return response 
  • This works, however do you know of a solution that keeps any/all of that code within the serializer classes? I'd rather not control serialization from the view. – L. W. May 11 '16 at 17:12
  • @L.W. I am currently not aware of a method to do this using just the serializers. Will update my answer upon knowing any such method. – Rahul Gupta May 11 '16 at 18:04
  • Accepting this as the answer. Couldn't find any other solution either. – L. W. May 12 '16 at 16:27

Depending on how you'll want incoming data and your models, you can:

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.