Convert a queryset to json using tastypie resource

后端 未结 1 1893
[愿得一人]
[愿得一人] 2021-01-05 04:45

I have a tastypie resource for a model. I also have a view which comes up with a queryset which needs to be serialised and sent to client. I am looking for a way to let tast

相关标签:
1条回答
  • 2021-01-05 05:43

    This was bugging me too, but I think I found the answer after looking through tastypie's code on github.

    This will make a bunch of bundles.

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

    This will perform the dehydration.

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

    This will perform the serialization.

    Resource.serialize(None, data, 'application/json'),
    
    0 讨论(0)
提交回复
热议问题