Search code examples
djangopython-2.7tastypie

Django tastypie: Add new Array of objects Next to "objects" and "meta" in Api output


In Django tastypie, i have this output from an Api.

{ 
 "meta": {
    "limit": 1000,
    "next": null,
    "offset": 0,
    "previous": null,
    "total_count": 2
  },
 "objects": [
    {},
    {}
   ]
}

i want to extend the output data. what i need is add new Array of objects named "Images" next to "objects" that is separate from "objects" data, like this:

{ 
 "meta": {
    "limit": 1000,
    "next": null,
    "offset": 0,
    "previous": null,
    "total_count": 2
  },
 "objects": [
    {},
    {}
   ],
 "images": [
    {},
    {}
   ]
}

How can i Implement it with tastypie of Django?


Solution

  • For list responses you can use Resource.alter_list_data_to_serialize

    class PageDataAddition(object):
        def alter_list_data_to_serialize(self, request, data):
            data['page'] = {'your_data': True}
            return data
    
    class ItemResource(PageDataAddition, ModelResource):
        ...