Is it possible to set django-tastypie object key?

By default, when using django-tastypie and fetching a list of resources, the response has the format:

{ "meta": { "limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 3 }, "objects": [{ "body": "Welcome to my blog!", "id": "1", "pub_date": "2011-05-20T00:46:38", "resource_uri": "/api/v1/entry/1/", "slug": "first-post", "title": "First Post", "user": "/api/v1/user/1/" }, ... ] } 

I delved into the documentation and looked and looked, but I can’t find any meta parameter or setting to change the “objects” key to actually describe the returned elements. For example, let's say I have a list of places in one api call and a list of people in another. I would like to distinguish a key from "locations" and "people". The real reason for this is that I am using RestKit for iOS and want to be able to establish multiple mappings.

+6
source share
1 answer

The alter_ * resource keys can be used to change the data structure.

An example of a resource using "locations":

 class MyLocationsResource(ModelResource): def alter_list_data_to_serialize(self, request, data): data['locations'] = data['objects'] del data['objects'] return data def alter_deserialized_list_data(self, request, data): data['objects'] = data['locations'] del data['locations'] return data 
+15
source

Source: https://habr.com/ru/post/908978/


All Articles