Django tastypie, it is possible to return only metadata for the request

I have the Django Tastypie API installed.

I would like to query the database for the number of objects matching the name.

Is this possible on an existing model resource, or do I need to set up a new resource to handle this particular case? (Is this data usually returned in the metadata part of the result? Is there an option to return this from the parameters?)

So, if my url usually looks like this:

http://127.0.0.1:8000/api/v1/library/?name__icontains=ABC 

Is it possible to add a parameter or change the URL so that it returns only metadata (I want only the number of libraries returned that have a name containing "ABC"), and not all objects?

+6
source share
1 answer

You can pass the get parameter:

 http://127.0.0.1:8000/api/v1/library/?name__icontains=ABC&meta_only=true 

and add this method to your resource:

 def alter_list_data_to_serialize(self, request, data): if request.GET.get('meta_only'): return {'meta': data['meta']} return data 

: http://django-tastypie.readthedocs.org/en/latest/resources.html#alter-list-data-to-serialize

+6
source

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


All Articles