Skip to main content

A multipart (multipart/form-data) renderer for Django Rest Framework

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_multipart_renderer-1.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_multipart_renderer-1.0.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file drf_multipart_renderer-1.0.1.tar.gz.

File metadata

  • Download URL: drf_multipart_renderer-1.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for drf_multipart_renderer-1.0.1.tar.gz
Algorithm Hash digest
SHA256 de3762b374b3867f17bc333a35e2729dd1241d1064383e3676c7f1ce4bbe816a
MD5 9a6fa88dd53ae8c57111cff65021a65b
BLAKE2b-256 c2a40cdb652dedd760eb02a9d523b89b5c1d7169a0eaecd989bed9a73a30cf58

See more details on using hashes here.

File details

Details for the file drf_multipart_renderer-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_multipart_renderer-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef323e8a90408d2143599791ed4be9ac8bca0644778a988a26c1e5bcca493336
MD5 1cb246e10f8725c47f6da1d3fbabea25
BLAKE2b-256 b50785231ac46aa4f5e58fbe0ec8e0bb4917601c10e4c06242beddaa41f93cfd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page