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-xmlVerified import paths — ran on the pinned version, not inferred.
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).
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.
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.
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.
Ensure that API clients explicitly set the `Content-Type` header to `application/xml` when submitting XML data to your endpoints.
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.