Registry / web-framework / djangorestframework-xml

djangorestframework-xml

JSON →
library2.0.0pypypiunverified

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.

pip install djangorestframework-xml
INSTALL
IMPORT
SIG · DJANGORESTFRAMEWOR
D
djangorestframework-xml
web-frameworkpythonv2.0.0
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

XMLParser
from rest_framework_xml.parsers import XMLParser
from rest_framework_xml.parsers import XMLParser
XMLRenderer
from rest_framework_xml.renderers import XMLRenderer
XMLEncoder
from rest_framework_xml.encoders import XMLEncoder

To enable XML support globally, add `XMLParser` and `XMLRenderer` to your `REST_FRAMEWORK` settings in `settings.py`. For view-specific control, you can apply `@parser_classes` and `@renderer_classes` decorators to function-based views or set `parser_classes` and `renderer_classes` attributes in class-based views. Ensure `rest_framework_xml` is in your `INSTALLED_APPS` (though often not strictly necessary for just renderers/parsers, it's good practice).

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})
Debug
Known issues
breakingVersion 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+.
fix
Upgrade your Python environment to Python 3.5+ (preferably 3.8+ for current Django/DRF compatibility) and ensure your Django is 2.2+ and Django REST Framework is 3.11+ before upgrading to djangorestframework-xml v2.0.0 or newer.
affects: <2.0.0
gotchaThe 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.
fix
For specific XML output requirements, extend `XMLRenderer` and/or `XMLParser` to customize the `root_tag_name`, `item_tag_name`, or completely override rendering/parsing logic to match your desired XML structure.
affects: All
gotchadjangorestframework-xml requires the `defusedxml` package for security purposes. Failing to install it could expose your application to XML-related vulnerabilities.
fix
Ensure `defusedxml` is explicitly installed in your environment: `pip install defusedxml`. It is listed as a required dependency, but an explicit check is good practice.
affects: All
gotchaClients 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.
fix
Ensure that API clients explicitly set the `Content-Type` header to `application/xml` when submitting XML data to your endpoints.
affects: All
gotchaThe default `XMLRenderer` might have limitations with deeply nested data structures or specific XML attribute requirements. Advanced XML structures may require further customization.
fix
For complex nested XML or precise attribute placement, you may need to fine-tune your DRF serializers or create a custom `XMLRenderer` that extends the base to handle your specific needs.
affects: All
Upgrade
Version history
2.0.0latest on PyPI · released Apr 12, 2020
Audit
Dependencies
DjangorequiredRequired for core Django project functionality. Version 2.2+ is required as of djangorestframework-xml v2.0.0.
djangorestframeworkrequiredThe core framework that djangorestframework-xml extends. Version 3.11+ is required as of djangorestframework-xml v2.0.0.
defusedxmlrequiredRequired for security against XML vulnerabilities when parsing XML content.
Agent activity
20 hits · last 30 days
node
18
Resources
djangorestframework-xml — pip install djangorestframework-xml · libregistry