Django REST Framework XML
djangorestframework-xml provides XML parsing and rendering support for Django REST Framework. It was previously part of DRF itself but is now maintained as a separate, third-party package. The library is currently at version 2.0.0 and offers a stable way to integrate XML into DRF APIs, with releases occurring periodically to maintain compatibility with newer Django and DRF versions.
Warnings
- breaking Version 2.0.0 of djangorestframework-xml dropped support for Python 2.x. It also updated its minimum dependency requirements to Django 2.2+ and Django REST Framework 3.11+.
- gotcha The default XML format generated by djangorestframework-xml is an 'ad-hoc' informal style. If your API has specific XML schema requirements (e.g., RSS, Atom, or a custom DTD), the default renderer/parser may not be sufficient. You may need to implement custom XML renderers and parsers.
- gotcha djangorestframework-xml requires the `defusedxml` package for security purposes. Failing to install it could expose your application to XML-related vulnerabilities.
- gotcha Clients making requests with XML payloads must send the correct `Content-Type` header (e.g., `application/xml`) for `XMLParser` to correctly process the data. Without it, DRF might default to `JSONParser` or another parser, leading to errors.
- gotcha The default `XMLRenderer` might have limitations with deeply nested data structures or specific XML attribute requirements. Advanced XML structures may require further customization.
Install
-
pip install djangorestframework-xml
Imports
- XMLParser
from rest_framework_xml.parsers import XMLParser
- XMLRenderer
from rest_framework_xml.renderers import XMLRenderer
- StringIO
from rest_framework.compat import StringIO
Quickstart
import os
# settings.py
# Add 'rest_framework_xml' to INSTALLED_APPS if using specific template renderers or custom configurations
INSTALLED_APPS = [
# ... other Django apps
'rest_framework',
'rest_framework_xml',
]
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework_xml.parsers.XMLParser',
],
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework_xml.renderers.XMLRenderer',
'rest_framework.renderers.BrowsableAPIRenderer' # Optional: for DRF's browsable API
],
}
# views.py example
from rest_framework.decorators import api_view, renderer_classes, parser_classes
from rest_framework.response import Response
from rest_framework_xml.parsers import XMLParser
from rest_framework_xml.renderers import XMLRenderer
@api_view(['GET', 'POST'])
@parser_classes([XMLParser])
@renderer_classes([XMLRenderer])
def example_xml_view(request):
""" A view that can accept/return XML content. """
if request.method == 'GET':
data = {'message': 'Hello from XML API!', 'status': 'success'}
return Response(data)
elif request.method == 'POST':
received_data = request.data
# Process received_data (which will be parsed from XML)
return Response({'received_data': received_data, 'processed': True})