django - Tastypie to respond for every requests in Json format -


i working on api project, i'm using tastypie 9.9.0. want response in json format put, post , delete operations.

the existing responses status 201 created, status 204 no content, status 410 gone fine.

it must respond in custom format. example

1.  {         "resource_name": "user",         "action":"password_reset",         "status": "success"      }  2.  {       "resource_name": "todo",       "action":"insert",       "status":"sucess",     } 3.  {       "resource_name": "todo",       "action":"delete",       "status":"sucess",     } 

this code working on. dont know how add custom response messages

  class todoresource(modelresource):         user = fields.toonefield(userresource, 'user')          class meta:             queryset = todo.objects.all()             fields=['alert_time','description','status','user']             resource_name = 'todo'             filtering = {                          'user': all_with_relations,                          'alert_time': ['exact', 'range', 'gt', 'gte', 'lt', 'lte'],                         }             serializer = serializer()             authentication= myapikeyauthentication()             authorization=authorization()             always_return_data = true             allowed_methods = ['post','get','put','delete']           def obj_create(self, bundle,request=none, **kwargs):             if not request.user.is_superuser:                 try:                     bundle.data.pop('user')                 except:                     pass             return super(todoresource, self).obj_create(bundle, request, user=request.user)          def create_response(self, request, data):             """             extracts common "which-format/serialize/return-response" cycle.              useful shortcut/hook.             """             desired_format = self.determine_format(request)             serialized = self.serialize(request, data, desired_format)             return httpresponse(content=serialized, content_type=build_content_type(desired_format))          def apply_authorization_limits(self, request, object_list):             return object_list.filter(user=request.user) 

you can add/modify custom data in get_list(request, **kwargs) and/or get_object(request, **kwargs)

for example,

import json django.http import httpresponse  class todoresource(modelresource):     # ... rest of code     def get_list(self, request, **kwargs):         resp = super(todoresource, self).get_list(request, **kwargs)         data = json.loads(resp.content)          # ... rest of code          data['meta']['resource_name'] = self._meta.resource_name         data['meta']['action'] = request.method         data['meta']['status'] = any_status          # ... rest of code          data = json.dumps(data)          return httpresponse(data, mimetype='application/json', status=any_status) 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -