Smart nested parser for Django REST Framework (JSON + multipart + form-data)
Project description
DRF Smart Nested Parser
A custom parser class for Django REST Framework (DRF) that converts bracket-notation form keys into properly nested Python structures.
It transparently handles:
multipart/form-dataapplication/x-www-form-urlencodedapplication/json(delegated to DRF's defaultJSONParser)
This allows frontend clients to submit nested data using standard HTML form conventions while preserving DRF’s normal behavior.
Why?
Django REST Framework does not natively support nested bracket notation in form submissions such as:
user[name]
items[0][title]
This parser bridges that gap, enabling clients (Browsers, Postman, Bruno, mobile apps) to send nested form data without switching to JSON payloads.
Features
- Parses nested keys like
user[name],items[0][title] - Builds proper nested
dictandliststructures - Preserves uploaded files in
request.FILES - Delegates JSON requests to DRF’s native
JSONParser - Automatically casts numeric string values to
intwhen possible - Works seamlessly as a global or per-view parser
Requirements
- Python >= 3.8
- Django >= 4.2
- djangorestframework >= 3.14
Installation
Available on PyPI:
pip install drf_smart_nested_parser
Quick Start
Global configuration
# settings.py
REST_FRAMEWORK = {
"DEFAULT_PARSER_CLASSES": [
"drf_smart_nested_parser.SmartNestedParser",
]
}
This replaces DRF’s default parsers while internally delegating to:
JSONParserMultiPartParserFormParser
Per-view usage
from rest_framework.views import APIView
from drf_smart_nested_parser import SmartNestedParser
class MyView(APIView):
parser_classes = [SmartNestedParser]
Example
Incoming request
(multipart/form-data or application/x-www-form-urlencoded)
user[name] = Amir
user[age] = 30
items[0][title] = Book
items[0][price] = 120
items[1][title] = Pen
Parsed result (request.data)
{
"user": {
"name": "Amir",
"age": 30
},
"items": [
{"title": "Book", "price": 120},
{"title": "Pen"}
]
}
Uploaded files remain accessible via:
request.FILES
Behavior
| Content-Type | Behavior |
|---|---|
application/json |
Uses DRF's JSONParser without modification |
multipart/form-data |
Parsed using MultiPartParser, then keys are converted to nested structures |
application/x-www-form-urlencoded |
Parsed using FormParser, then keys are converted to nested structures |
Numeric string values are automatically cast to int when possible.
Key Rules
Valid examples:
items[0][name]
user[address][city]
Invalid example:
[0][name]
Keys must start with a string root.
File Handling
File objects are preserved and returned inside DataAndFiles.files.
They are not modified or transformed.
Use Cases
- Complex nested form submissions
- Frontend frameworks sending bracket-notation keys
- Multipart requests combining nested data + file uploads
- Replacing JSON with form submissions while keeping structure
Project Structure
drf_smart_nested_parser/
├── parsers.py
├── utils.py
└── __init__.py
License
MIT License
Contributing
Pull requests are welcome. Please open an issue first to discuss major changes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drf_smart_nested_parser-0.2.0.tar.gz.
File metadata
- Download URL: drf_smart_nested_parser-0.2.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1fac031cead811db4b9e0f80b60131c730fa68342800a3b2fff314a6ddb0924
|
|
| MD5 |
cc82b2e940aeff9bd1f366fc5895dba3
|
|
| BLAKE2b-256 |
84fd26b66aaecaba73d3b5c7b17ef56d3a81a9a2698db05f59b2aadf177aaae9
|
File details
Details for the file drf_smart_nested_parser-0.2.0-py3-none-any.whl.
File metadata
- Download URL: drf_smart_nested_parser-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ff7f8b3538fc7307499a733c72bab1b983535a582d5b100b6ec1a9d4ca98420
|
|
| MD5 |
933b7a02ee130cc8954d5bf3ff84c9fe
|
|
| BLAKE2b-256 |
da256a6359b4aab7f5fab3c642105806c9ca60f15dedc0e089ff5428ec67f075
|