Smart nested parser for DRF (JSON + multipart + files)
Project description
DRF Smart Nested Parser
A simple parser for Django REST Framework that converts form-data and application/x-www-form-urlencoded inputs into nested structures (dict / list). For JSON, it uses the default DRF JSON parser without additional conversion.
Installation
If you're using this package locally, ensure it is importable from your project. If published on PyPI, install it by the package name.
Usage
Add to DRF settings
# settings.py
REST_FRAMEWORK = {
"DEFAULT_PARSER_CLASSES": [
"drf_smart_nested_parser.SmartNestedParser",
]
}
Or only for a single view
from rest_framework.views import APIView
from drf_smart_nested_parser import SmartNestedParser
class MyView(APIView):
parser_classes = [SmartNestedParser]
Input and Output
Example for multipart/form-data or application/x-www-form-urlencoded
Form keys with bracket notation ([]) are converted to nested structures:
user[name] = Amir
user[age] = 30
items[0][title] = Book
items[0][price] = 120
items[1][title] = Pen
Output:
{
"user": {
"name": "Amir",
"age": 30
},
"items": [
{"title": "Book", "price": 120},
{"title": "Pen"}
]
}
Note: If a value can be converted to a number, it will be cast to int.
Parser Behavior
- If
Content-Typeincludesapplication/json, DRF'sJSONParseris used. - If
Content-Typeincludesmultipart/form-data, data is parsed and keys are converted to nested structures. - Otherwise (e.g.
application/x-www-form-urlencoded),FormParseris used and the same conversion applies.
Limitations and Notes
- Keys should start with a string root (e.g.
items[0]should be used as part of a root likeitems[0][name]or at leastitems[0]alongside a textual root). A valid example:items[0][name]. - File values in
multipart/form-dataare kept infilesand are not modified.
Development
Code layout:
parsers.py:SmartNestedParserimplementationutils.py:parse_nested_keyshelper
License
MIT
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.1.0.tar.gz.
File metadata
- Download URL: drf_smart_nested_parser-0.1.0.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f647d07b7de4ee5ec6d79a9417267dbb6a27a3ef297690e5bb125d14a44da666
|
|
| MD5 |
a73d404a0c4c27b453e16bdfe55529b3
|
|
| BLAKE2b-256 |
dab2622e35f428db0e26410f879af99c5e157ce6dcea0d61bc35dd46589bd9b2
|
File details
Details for the file drf_smart_nested_parser-0.1.0-py3-none-any.whl.
File metadata
- Download URL: drf_smart_nested_parser-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.3 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 |
0784cfda3acc21881a5f88818b8c80d1166510f3bed4870b9bcc9afd9c24ad6d
|
|
| MD5 |
a23c1537aeb65e7009a821ac83d429d1
|
|
| BLAKE2b-256 |
206efeeb69e7ce1e71abc1a7c4df76679fda2033438035020ac85f1f916c50ad
|