मैं django और django-rest-framework का उपयोग कर सभी जेसन प्रतिक्रियाओं में रूट तत्व जोड़ने का सबसे अच्छा तरीका निर्धारित करने का प्रयास कर रहा हूं।जेसन प्रतिक्रिया (डीजेंगो-रेस्ट-फ्रेमवर्क) में रूट तत्व जोड़ना
मैं एक कस्टम रेंडरर जोड़ने लगता है कि सबसे अच्छा तरीका है पूरा करने के लिए मैं क्या हासिल करना चाहते है और यह है कि क्या मैं अब तक के साथ आए हैं है:
from rest_framework.renderers import JSONRenderer
class CustomJSONRenderer(JSONRenderer):
#override the render method
def render(self, data, accepted_media_type=None, renderer_context=None):
#call super, as we really just want to mess with the data returned
json_str = super(CustomJSONRenderer, self).render(data, accepted_media_type, renderer_context)
root_element = 'contact'
#wrap the json string in the desired root element
ret = '{%s: %s}' % (root_element, json_str)
return ret
मुश्किल हिस्सा अब गतिशील root_element
स्थापित कर रही है आधारित इस विचार पर कि render()
से कहा जा रहा है।
किसी भी संकेत दिए गए/सलाह बहुत सराहना की जाएगी,
चीयर्स
धन्यवाद एक विचार के लिए ever.wakeful, मैं भी मैं इसे कैसे ExtJs दृश्यपटल के साथ उपयोग के बारे में पोस्ट लिखा (वहां मुझे मूल तत्व को अलग-अलग कॉल करने की आवश्यकता नहीं है। और मुझे रूट तत्व के अंदर paginator.count होना चाहिए) http://kaygorodov.github.io/2014/02/21/extjs-django-rest-framework -root-element.html –