user825904 user825904 - 11 months ago 44
Python Question

How to add extra object to tasty pie return json in python django

In Django project i get two objects when i receive the JSON response


This is my Resource

class MyResource(ModelResource):
def dehydrate(self, bundle):["absolute_url"] = bundle.obj.get_absolute_url()['myfields'] = MyDataFields
return bundle
class Meta:

queryset = MyData.objects.all()
resource_name = 'weather'
serializer = Serializer(formats=['json'])
ordering = MyDataFields

now i want to other field in json like


but if i do the above way then that field is added to every object like


how can i do

Answer Source

One way to do this is by overriding Tastypie ModelResource's get_list method.

import json
from django.http import HttpResponse


class MyResource(ModelResource):


    def get_list(self, request, **kwargs):
        resp = super(MyResource, self).get_list(request, **kwargs)

        data = json.loads(resp.content)

        data['myfields'] = MyDataFields

        data = json.dumps(data)

        return HttpResponse(data, content_type='application/json', status=200)