Override JSONSerializer on django rest platform

I am trying to apply this fix in my django rest system Adding the root element to json response (django-rest-framework)

But I'm not sure how to override json serializer in django rest framework, any help would be great.

The end result will be the root name of the node in Json, because right now it is just an array of objects without root name ie

Not like that

[{"Foo": "bar"}]

I need it to be like this

{"element": [{"foo": "bar"}]}

to make it work with Ember JS

thanks

+6
source share
1 answer

I think that you have your answer there, in the message that you indicated.

You need to define custom JSON rendering

from rest_framework.renderers import JSONRenderer class EmberJSONRenderer(JSONRenderer): def render(self, data, accepted_media_type=None, renderer_context=None): data = {'element': data} return super(EmberJSONRenderer, self).render(data, accepted_media_type, renderer_context) 

and use it as the default rendering either in the settings or as an explicitly defined render for viewing, for example:

 class MyView(APIView): renderer_classes = (EmberJSONRenderer, ) # ... 
+8
source

Source: https://habr.com/ru/post/959477/


All Articles