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 (io.IOBase) 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 uses a custom boundary string: BoUnDaRyStRiNgetpvelarptriznzsespgfmagoxpjpjluxkwqroqgsilzbdfsfgffddg

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.0.tar.gz (5.2 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.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for drf_multipart_renderer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c7f2d89b37f22916c6bf93bc1a9a09e6d9610c7f78f11f0e718af84a390bc34c
MD5 53982410e971bff065a019da1df3bc9d
BLAKE2b-256 01151728ce7a5fb14ef0b432b46ba6077527dffb401f2e0a5a7911e311d64988

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for drf_multipart_renderer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b47735f891525f34d7af018192a1ce76cc383084d64ad6e7e7ac98f96e739e7
MD5 2da698d2aeefd129506aa408b2ae9877
BLAKE2b-256 ed9f96a2eabbce39076a61eb8e6520d9d219a2f999a7729d22a9fab32542c933

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