Skip to main content

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-Type includes application/json, DRF's JSONParser is used.
  • If Content-Type includes multipart/form-data, data is parsed and keys are converted to nested structures.
  • Otherwise (e.g. application/x-www-form-urlencoded), FormParser is 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 like items[0][name] or at least items[0] alongside a textual root). A valid example: items[0][name].
  • File values in multipart/form-data are kept in files and are not modified.

Development

Code layout:

  • parsers.py: SmartNestedParser implementation
  • utils.py: parse_nested_keys helper

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_smart_nested_parser-0.1.0.tar.gz (2.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_smart_nested_parser-0.1.0-py3-none-any.whl (2.3 kB view details)

Uploaded Python 3

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

Hashes for drf_smart_nested_parser-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f647d07b7de4ee5ec6d79a9417267dbb6a27a3ef297690e5bb125d14a44da666
MD5 a73d404a0c4c27b453e16bdfe55529b3
BLAKE2b-256 dab2622e35f428db0e26410f879af99c5e157ce6dcea0d61bc35dd46589bd9b2

See more details on using hashes here.

File details

Details for the file drf_smart_nested_parser-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_smart_nested_parser-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0784cfda3acc21881a5f88818b8c80d1166510f3bed4870b9bcc9afd9c24ad6d
MD5 a23c1537aeb65e7009a821ac83d429d1
BLAKE2b-256 206efeeb69e7ce1e71abc1a7c4df76679fda2033438035020ac85f1f916c50ad

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