Django tastypie, possible to return only metadata for a query
Asked Answered
S

1

7

I have a Django Tastypie API set up.

I would like to query the database for the count of objects that match a name.

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

So if my url is usually like this:

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

Can I add a parameter or change the url so that it only returns the metadata (I only want the number of libraries returned that have a name containing "ABC") rather than all the objects?

Stumper answered 23/8, 2013 at 10:20 Comment(0)
C
7

You can pass in a get param:

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

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

Callup answered 23/8, 2013 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.