Django REST API in XML & JSON












0














How could I produce Django REST API in XML & JSON at the same time from a same model?



I have a model and need to create 2 different outputs from that model, one in XML and one in JSON.










share|improve this question
























  • That should be pretty straight forward by specifying DEFAULT_RENDERER_CLASSES in settings.py to the appropriate values. What do you exactly mean by same time? Do you want XML and JSON output generally for the whole of your REST API (all models), or just for one single particular model?
    – cezar
    Nov 20 at 8:01










  • I have few models , but i want to create two type of api from only one model, and for other model JSON type is enough.
    – Zahid Uan Nabi
    Nov 20 at 8:06
















0














How could I produce Django REST API in XML & JSON at the same time from a same model?



I have a model and need to create 2 different outputs from that model, one in XML and one in JSON.










share|improve this question
























  • That should be pretty straight forward by specifying DEFAULT_RENDERER_CLASSES in settings.py to the appropriate values. What do you exactly mean by same time? Do you want XML and JSON output generally for the whole of your REST API (all models), or just for one single particular model?
    – cezar
    Nov 20 at 8:01










  • I have few models , but i want to create two type of api from only one model, and for other model JSON type is enough.
    – Zahid Uan Nabi
    Nov 20 at 8:06














0












0








0







How could I produce Django REST API in XML & JSON at the same time from a same model?



I have a model and need to create 2 different outputs from that model, one in XML and one in JSON.










share|improve this question















How could I produce Django REST API in XML & JSON at the same time from a same model?



I have a model and need to create 2 different outputs from that model, one in XML and one in JSON.







json django xml django-rest-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 8:34









cezar

5,49332454




5,49332454










asked Nov 20 at 7:37









Zahid Uan Nabi

5716




5716












  • That should be pretty straight forward by specifying DEFAULT_RENDERER_CLASSES in settings.py to the appropriate values. What do you exactly mean by same time? Do you want XML and JSON output generally for the whole of your REST API (all models), or just for one single particular model?
    – cezar
    Nov 20 at 8:01










  • I have few models , but i want to create two type of api from only one model, and for other model JSON type is enough.
    – Zahid Uan Nabi
    Nov 20 at 8:06


















  • That should be pretty straight forward by specifying DEFAULT_RENDERER_CLASSES in settings.py to the appropriate values. What do you exactly mean by same time? Do you want XML and JSON output generally for the whole of your REST API (all models), or just for one single particular model?
    – cezar
    Nov 20 at 8:01










  • I have few models , but i want to create two type of api from only one model, and for other model JSON type is enough.
    – Zahid Uan Nabi
    Nov 20 at 8:06
















That should be pretty straight forward by specifying DEFAULT_RENDERER_CLASSES in settings.py to the appropriate values. What do you exactly mean by same time? Do you want XML and JSON output generally for the whole of your REST API (all models), or just for one single particular model?
– cezar
Nov 20 at 8:01




That should be pretty straight forward by specifying DEFAULT_RENDERER_CLASSES in settings.py to the appropriate values. What do you exactly mean by same time? Do you want XML and JSON output generally for the whole of your REST API (all models), or just for one single particular model?
– cezar
Nov 20 at 8:01












I have few models , but i want to create two type of api from only one model, and for other model JSON type is enough.
– Zahid Uan Nabi
Nov 20 at 8:06




I have few models , but i want to create two type of api from only one model, and for other model JSON type is enough.
– Zahid Uan Nabi
Nov 20 at 8:06












1 Answer
1






active

oldest

votes


















3














If you need a custom behavior for just a particular model, you can specify the renderer_classes only in the view for that model.



Assuming you have a model, let's call it Foo:



# models.py
class Foo(models.Model):
# properties


you can do this in your views.py:



from rest_framework.renderers import JSONRenderer
from rest_framework_xml.renderers import XMLRenderer
from rest_framework.views import APIView

class FooView(APIView):
renderer_classes = (JSONRenderer, XMLRenderer)
# the rest


The XMLRenderer is not anymore integral part of the Django REST Framework and has to be installed as an additional package:



$ pip install djangorestframework-xml


The offical documentation describes the use of renderers.






share|improve this answer





















  • how to accomplish this in generic views ??
    – rammanoj
    Nov 20 at 8:26










  • Generic views extend the APIView, so there is no difference.
    – cezar
    Nov 20 at 8:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388258%2fdjango-rest-api-in-xml-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














If you need a custom behavior for just a particular model, you can specify the renderer_classes only in the view for that model.



Assuming you have a model, let's call it Foo:



# models.py
class Foo(models.Model):
# properties


you can do this in your views.py:



from rest_framework.renderers import JSONRenderer
from rest_framework_xml.renderers import XMLRenderer
from rest_framework.views import APIView

class FooView(APIView):
renderer_classes = (JSONRenderer, XMLRenderer)
# the rest


The XMLRenderer is not anymore integral part of the Django REST Framework and has to be installed as an additional package:



$ pip install djangorestframework-xml


The offical documentation describes the use of renderers.






share|improve this answer





















  • how to accomplish this in generic views ??
    – rammanoj
    Nov 20 at 8:26










  • Generic views extend the APIView, so there is no difference.
    – cezar
    Nov 20 at 8:29
















3














If you need a custom behavior for just a particular model, you can specify the renderer_classes only in the view for that model.



Assuming you have a model, let's call it Foo:



# models.py
class Foo(models.Model):
# properties


you can do this in your views.py:



from rest_framework.renderers import JSONRenderer
from rest_framework_xml.renderers import XMLRenderer
from rest_framework.views import APIView

class FooView(APIView):
renderer_classes = (JSONRenderer, XMLRenderer)
# the rest


The XMLRenderer is not anymore integral part of the Django REST Framework and has to be installed as an additional package:



$ pip install djangorestframework-xml


The offical documentation describes the use of renderers.






share|improve this answer





















  • how to accomplish this in generic views ??
    – rammanoj
    Nov 20 at 8:26










  • Generic views extend the APIView, so there is no difference.
    – cezar
    Nov 20 at 8:29














3












3








3






If you need a custom behavior for just a particular model, you can specify the renderer_classes only in the view for that model.



Assuming you have a model, let's call it Foo:



# models.py
class Foo(models.Model):
# properties


you can do this in your views.py:



from rest_framework.renderers import JSONRenderer
from rest_framework_xml.renderers import XMLRenderer
from rest_framework.views import APIView

class FooView(APIView):
renderer_classes = (JSONRenderer, XMLRenderer)
# the rest


The XMLRenderer is not anymore integral part of the Django REST Framework and has to be installed as an additional package:



$ pip install djangorestframework-xml


The offical documentation describes the use of renderers.






share|improve this answer












If you need a custom behavior for just a particular model, you can specify the renderer_classes only in the view for that model.



Assuming you have a model, let's call it Foo:



# models.py
class Foo(models.Model):
# properties


you can do this in your views.py:



from rest_framework.renderers import JSONRenderer
from rest_framework_xml.renderers import XMLRenderer
from rest_framework.views import APIView

class FooView(APIView):
renderer_classes = (JSONRenderer, XMLRenderer)
# the rest


The XMLRenderer is not anymore integral part of the Django REST Framework and has to be installed as an additional package:



$ pip install djangorestframework-xml


The offical documentation describes the use of renderers.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 8:15









cezar

5,49332454




5,49332454












  • how to accomplish this in generic views ??
    – rammanoj
    Nov 20 at 8:26










  • Generic views extend the APIView, so there is no difference.
    – cezar
    Nov 20 at 8:29


















  • how to accomplish this in generic views ??
    – rammanoj
    Nov 20 at 8:26










  • Generic views extend the APIView, so there is no difference.
    – cezar
    Nov 20 at 8:29
















how to accomplish this in generic views ??
– rammanoj
Nov 20 at 8:26




how to accomplish this in generic views ??
– rammanoj
Nov 20 at 8:26












Generic views extend the APIView, so there is no difference.
– cezar
Nov 20 at 8:29




Generic views extend the APIView, so there is no difference.
– cezar
Nov 20 at 8:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388258%2fdjango-rest-api-in-xml-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out