|
1 | | -# nested-multi-dimentional-parser |
| 1 | +# Nested-multipart-parser |
2 | 2 |
|
3 | | -Parser for nested data in multipart form |
| 3 | +Parser for nested data in multipart form, you can use it anyways, and you have a django rest framework integration |
| 4 | + |
| 5 | +# Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install nested-multipart-parser |
| 9 | +``` |
4 | 10 |
|
5 | 11 | # Usage |
6 | 12 |
|
| 13 | +## What is doing |
| 14 | + |
| 15 | +the parser take the request data and transform to dictionary |
| 16 | + |
| 17 | +exemple: |
| 18 | + |
7 | 19 | ```python |
8 | | -from nested_multipart_parser import parser |
9 | | -... |
| 20 | +# input |
| 21 | +{ |
| 22 | + 'title': 'title', |
| 23 | + 'date': "time", |
| 24 | + 'simple_object[my_key]': 'title' |
| 25 | + 'simple_object[my_list][0]': True |
| 26 | + 'langs[0][id]': 666, |
| 27 | + 'langs[0][title]': 'title', |
| 28 | + 'langs[0][description]': 'description', |
| 29 | + 'langs[0][language]': "language", |
| 30 | + 'langs[1][id]': 4566, |
| 31 | + 'langs[1][title]': 'title1', |
| 32 | + 'langs[1][description]': 'description1', |
| 33 | + 'langs[1][language]': "language1" |
| 34 | +} |
10 | 35 |
|
11 | | -class YourViewSet(viewsets.ViewSet): |
12 | | - parser_classes = (NestedMultipartParser,) |
| 36 | +# results are: |
| 37 | + { |
| 38 | + 'title': 'title', |
| 39 | + 'date': "time", |
| 40 | + 'simple_object': { |
| 41 | + 'my_key': 'title', |
| 42 | + 'my_list': [ |
| 43 | + True |
| 44 | + ] |
| 45 | + }, |
| 46 | + 'langs': [ |
| 47 | + { |
| 48 | + 'id': 666, |
| 49 | + 'title': 'title', |
| 50 | + 'description': 'description', |
| 51 | + 'language': 'language' |
| 52 | + }, |
| 53 | + { |
| 54 | + 'id': 4566, |
| 55 | + 'title': 'title1', |
| 56 | + 'description': 'description1', |
| 57 | + 'language': 'language1' |
| 58 | + } |
| 59 | + ] |
| 60 | +} |
13 | 61 | ``` |
14 | 62 |
|
15 | | -To enable JSON and multipart |
| 63 | +## How is work |
| 64 | + |
| 65 | +for this working perfectly you need to follow this rules: |
| 66 | + |
| 67 | +- a first key need to be set ex: 'title[0]' or 'title', in both the first key is 'title' |
| 68 | +- each sub key need to enclose by brackets "[--your-key--]" |
| 69 | +- if sub key are a full number, is converted to list |
| 70 | +- if sub key is Not a number is converted to dictionary |
| 71 | +- the key can't be rewite |
| 72 | + ex: |
16 | 73 |
|
17 | 74 | ```python |
18 | | -from drf_nested_field_multipart import NestedMultipartParser |
19 | | -from rest_framework.parsers import JSONParser |
20 | | -from rest_framework import viewsets |
| 75 | + data = { |
| 76 | + 'title[0]': 'my-value' |
| 77 | + } |
| 78 | + # output |
| 79 | + output = { |
| 80 | + 'title': [ |
| 81 | + 'my-value' |
| 82 | + ] |
| 83 | + } |
21 | 84 |
|
22 | | -class YourViewSet(viewsets.ViewSet): |
23 | | - parser_classes = (JSONParser, NestedMultipartParser) |
| 85 | + # invalid key |
| 86 | + data = { |
| 87 | + 'title[688]': 'my-value' |
| 88 | + } |
| 89 | + # ERROR , you set a number is upper thans actual list |
| 90 | + |
| 91 | + |
| 92 | + # wrong format |
| 93 | + data = { |
| 94 | + 'title[0]]]': 'my-value', |
| 95 | + 'title[0': 'my-value', |
| 96 | + 'title[': 'my-value', |
| 97 | + 'title[]': 'my-value', |
| 98 | + '[]': 'my-value', |
| 99 | + } |
| 100 | + |
| 101 | + data = { |
| 102 | + 'title': 42, |
| 103 | + 'title[object]': 42 |
| 104 | + } |
| 105 | + # Error , title as alerady set by primitive value (int, boolean or string) |
| 106 | + |
| 107 | + # many element in list |
| 108 | + data = { |
| 109 | + 'title[0]': 'my-value', |
| 110 | + 'title[1]': 'my-second-value' |
| 111 | + } |
| 112 | + # output |
| 113 | + output = { |
| 114 | + 'title': [ |
| 115 | + 'my-value', |
| 116 | + 'my-second-value' |
| 117 | + ] |
| 118 | + } |
| 119 | + |
| 120 | + # converted to object |
| 121 | + data = { |
| 122 | + 'title[key0]': 'my-value', |
| 123 | + 'title[key7]': 'my-second-value' |
| 124 | + } |
| 125 | + # output |
| 126 | + output = { |
| 127 | + 'title': { |
| 128 | + 'key0': 'my-value', |
| 129 | + 'key7': 'my-second-value' |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + # you have no limit for chained key |
| 134 | + data = { |
| 135 | + 'the[0][chained][key][0][are][awesome][0][0]': 'im here !!' |
| 136 | + } |
| 137 | + # output |
| 138 | + output: { |
| 139 | + 'the': [ |
| 140 | + { |
| 141 | + 'chained':{ |
| 142 | + 'key': [ |
| 143 | + { |
| 144 | + 'are': { |
| 145 | + 'awesome': |
| 146 | + [ |
| 147 | + [ |
| 148 | + 'im here !!' |
| 149 | + ] |
| 150 | + ] |
| 151 | + } |
| 152 | + } |
| 153 | + ] |
| 154 | + } |
| 155 | + } |
| 156 | + ] |
| 157 | + } |
24 | 158 | ``` |
25 | 159 |
|
26 | | -# Installation |
| 160 | +# How to use it |
| 161 | + |
| 162 | +## for every framwork |
27 | 163 |
|
28 | | -`pip install drf-nested-field-multipart` |
| 164 | +```python |
| 165 | +from nested_multipart_parser import NestedParser |
| 166 | + |
| 167 | +def my_view(): |
| 168 | + parser = NestedParser(data) |
| 169 | + if parser.is_valid(): |
| 170 | + validate_data = parser.validate_data |
| 171 | + ... |
| 172 | + else: |
| 173 | + print(parser.errors) |
| 174 | + |
| 175 | +``` |
| 176 | + |
| 177 | +## for django rest framwork |
| 178 | + |
| 179 | +```python |
| 180 | +from nested_multipart_parser.drf import DrfNestedParser |
| 181 | +... |
| 182 | + |
| 183 | +class YourViewSet(viewsets.ViewSet): |
| 184 | + parser_classes = (DrfNestedParser,) |
| 185 | +``` |
0 commit comments