Convert a set of requests to json using tastypie resource

I have a tastypie resource for a model. I also have a view in which a request appears that needs to be serialized and sent to the client. I am looking for a way to let the tastypie resource handle serialization and dehydration of a set of queries.

I see that I can pass one object to

[Resource.build_bundle(self, obj=None, data=None, request=None)][1] 

to create a package and then transfer the package to

 [Resource.full_dehydrate(self, bundle)][2] 

and finally let's call

 [Resource.serialize(self, request, data, format, options=None)][3] 

on dehydrated data.

But I want to convert a complete set of requests to json, and not just from a single object. Perhaps all I need is a way to convert a complete set of requests into a package.

Any help is appreciated!

+7
source share
1 answer

That overheard me too, but I think I found the answer by looking at the tastypie code on github.

This will create a bundle of packages.

 bundles = [Resource.build_bundle(obj=q, request=request) for q in Queryset] 

This will lead to dehydration.

 data = [Resource.full_dehydrate(bundle) for bundle in bundles] 

This will result in serialization.

 Resource.serialize(None, data, 'application/json'), 
+10
source

All Articles