Skip to main content

Django Rest Framework multipart renderer

A high-performance multipart (multipart/form-data) renderer for Django Rest Framework. This package allows you to return complex, nested data structures and binary files in a single multipart response.

Highlights

  • Response renderer: Suitable as a DRF Response Renderer
  • Smart Type Mapping: Automatically determines Content-Type for different Python objects.
  • File Support: Handles file streams and automatically selects MIME types.
  • JSON Integration: Serializes primitives and dictionaries as application/json parts.
  • List Flattening: Supports multiple values for a single key (e.g., multiple tags or images).

Install

pip install drf-multipart-renderer

Usage

To use this renderer, add it to your renderer_classes in a DRF View or ViewSet.

Basic Example

from rest_framework.views import APIView
from rest_framework.response import Response
from drf_multipart_renderer import MultipartRenderer

class MyView(APIView):
    renderer_classes = [MultipartRenderer]

    def get(self, request):
        data = {
            "title": "Project Alpha",
            "metadata": {"version": 1.0, "active": True},
            "file": open("report.pdf", "rb"),
            "tags": ["python", "django"],
            "number": 33
        }
        
        return Response(data)

Data Type Mapping Logic

The MultipartRenderer automatically determines the Content-Type of each part in the response based on the Python type of the dictionary value.

Python Type Target Content-Type Handling Behavior
String (str) text/plain Encoded as standard UTF-8 text.
Primitives (int, float, bool) application/json Serialized as a JSON value.
Dictionary (dict) application/json Serialized as a JSON object.
File (has a callable read method) guessed/type Uses the file object's content_type if exists, else guesses type from filename; defaults to application/octet-stream.
Iterables (list, tuple) Per-element type Flattens the collection into multiple entries using the same key name.
None application/json Serialized as null.

Nested Collection Note

If an iterable (like a list) contains another collection (like a list of lists or a list of dicts), the nested collection is automatically serialized as a JSON string within that form entry.

Technical Details

The renderer generates a random boundary for every response.

It adheres to standard multipart formatting, using \r\n line endings as required by the HTTP specification.

Every component of the multipart payload (including field names, text values, JSON objects, and headers) is encoded using UTF-8 to ensure universal character compatibility.

TESTS

To run the test suite for this package, use the following command:

 DJANGO_SETTINGS_MODULE="src.tests.settings" python -m django test

License

MIT

Metadata

Release files for drf-multipart-renderer 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-multipart-renderer 1.0.1
File Size Uploaded
drf_multipart_renderer-1.0.1.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-multipart-renderer 1.0.1
File Interpreter ABI Platform
drf_multipart_renderer-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.9 kB

Release files / drf_multipart_renderer-1.0.1.tar.gz

Download URL drf_multipart_renderer-1.0.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
de3762b374b3867f17bc333a35e2729dd1241d1064383e3676c7f1ce4bbe816a
BLAKE2b-256 checksum
How to use checksums
c2a40cdb652dedd760eb02a9d523b89b5c1d7169a0eaecd989bed9a73a30cf58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / drf_multipart_renderer-1.0.1-py3-none-any.whl

Download URL drf_multipart_renderer-1.0.1-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef323e8a90408d2143599791ed4be9ac8bca0644778a988a26c1e5bcca493336
BLAKE2b-256 checksum
How to use checksums
b50785231ac46aa4f5e58fbe0ec8e0bb4917601c10e4c06242beddaa41f93cfd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page