Search code examples
djangotastypie

alter the structure Tastypie uses in the list view


I have json list view that look like this:

{
"objects": [
{
  "active": false,
  "id": 4,
},
{
  "active": false,
  "id": 5,
}
]
}

I want to get rid of "objects" word, so that structure will look like this:

{
[
{
  "active": false,
  "id": 4,
},
{
  "active": false,
  "id": 5,
}
]
}

This link to docs has no clue in it


Solution

  • It's impossible. {} means dict. Dict needs key and value.

    I guess You need

    [
      {
        "active": false,
        "id": 4,
      },
      {
        "active": false,
        "id": 5,
      }
    ]
    

    If yes, overwrite Resource.alter_list_data_to_serialize function:

    def alter_list_data_to_serialize(self, request, data):
        return data[self._meta.collection_name]
    

    Paginator class need to be dict with field named Resouce._meta.collection_name.