{"id":6605,"library":"djangorestframework-xml","title":"Django REST Framework XML","description":"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.","status":"active","version":"2.0.0","language":"en","source_language":"en","source_url":"https://github.com/jpadilla/django-rest-framework-xml","tags":["django","rest framework","xml","parser","renderer","api"],"install":[{"cmd":"pip install djangorestframework-xml","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for core Django project functionality. Version 2.2+ is required as of djangorestframework-xml v2.0.0.","package":"Django","optional":false},{"reason":"The core framework that djangorestframework-xml extends. Version 3.11+ is required as of djangorestframework-xml v2.0.0.","package":"djangorestframework","optional":false},{"reason":"Required for security against XML vulnerabilities when parsing XML content.","package":"defusedxml","optional":false}],"imports":[{"symbol":"XMLParser","correct":"from rest_framework_xml.parsers import XMLParser"},{"symbol":"XMLRenderer","correct":"from rest_framework_xml.renderers import XMLRenderer"},{"note":"In older versions (prior to 1.0.1 and 1.4.0), there were issues with importing `StringIO` and `smart_text` from `rest_framework.compat`. These were fixed, and direct usage of Python's standard `io.StringIO` or appropriate text handling is now expected if needed. The library itself handles its internal compatibility.","wrong":"from rest_framework.compat import StringIO","symbol":"StringIO"}],"quickstart":{"code":"import os\n\n# settings.py\n# Add 'rest_framework_xml' to INSTALLED_APPS if using specific template renderers or custom configurations\nINSTALLED_APPS = [\n    # ... other Django apps\n    'rest_framework',\n    'rest_framework_xml',\n]\n\nREST_FRAMEWORK = {\n    'DEFAULT_PARSER_CLASSES': [\n        'rest_framework.parsers.JSONParser',\n        'rest_framework_xml.parsers.XMLParser',\n    ],\n    'DEFAULT_RENDERER_CLASSES': [\n        'rest_framework.renderers.JSONRenderer',\n        'rest_framework_xml.renderers.XMLRenderer',\n        'rest_framework.renderers.BrowsableAPIRenderer' # Optional: for DRF's browsable API\n    ],\n}\n\n# views.py example\nfrom rest_framework.decorators import api_view, renderer_classes, parser_classes\nfrom rest_framework.response import Response\nfrom rest_framework_xml.parsers import XMLParser\nfrom rest_framework_xml.renderers import XMLRenderer\n\n@api_view(['GET', 'POST'])\n@parser_classes([XMLParser])\n@renderer_classes([XMLRenderer])\ndef example_xml_view(request):\n    \"\"\" A view that can accept/return XML content. \"\"\"\n    if request.method == 'GET':\n        data = {'message': 'Hello from XML API!', 'status': 'success'}\n        return Response(data)\n    elif request.method == 'POST':\n        received_data = request.data\n        # Process received_data (which will be parsed from XML)\n        return Response({'received_data': received_data, 'processed': True})\n\n","lang":"python","description":"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)."},"warnings":[{"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.","message":"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+.","severity":"breaking","affected_versions":"<2.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"djangorestframework-xml requires the `defusedxml` package for security purposes. Failing to install it could expose your application to XML-related vulnerabilities.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure that API clients explicitly set the `Content-Type` header to `application/xml` when submitting XML data to your endpoints.","message":"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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"The default `XMLRenderer` might have limitations with deeply nested data structures or specific XML attribute requirements. Advanced XML structures may require further customization.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}