Reputation:
This is my app/api.py
from app.models import Person
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
class PersonResource(ModelResource):
class Meta:
queryset = Person.objects.all()
resource_name='person
# filtering = { "email" : ALL,"dob":ALL,"mobile":ALL}
filtering = {
'email': ALL,
'mobile': ALL,
'dob': ALL,
}
It returns json successfully at: /api/person/?format=json@[email protected]
But i would like to add one more string in json output as "status" which will be True when filtering was success and False when there is no entry in database corresponding to given email. How can i do that ?
Upvotes: 2
Views: 124
Reputation: 15864
The easiest way would be to define your own alter_list_data_to_serialize
method. The method is called right before returning response of the get_list
request, which is the one in the question, and should return the final dictionary that will be serialized in the response.
Assuming you are not restructuring the objects list metadata (tastypie's paginator wraps the objects' list in a dictionary where objects
points at the list, total_count
hold counts of total objects, etc.), you could do the following:
def alter_list_data_to_serialize(self, request, data):
data['status'] = data['total_count'] != 0
return data
Otherwise, if you are using a custom paginator and want to include the status
key in all of the resources, you could modify the Paginator.page()
method to add the status
key.
Upvotes: 3