Skip to content

Commit 7b60417

Browse files
author
renaud.cepre
committed
Update README.md
1 parent 8af77a7 commit 7b60417

File tree

1 file changed

+47
-86
lines changed

1 file changed

+47
-86
lines changed

README.md

Lines changed: 47 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
[![build](https://github.com/remigermain/nested-multipart-parser/actions/workflows/main.yml/badge.svg)](https://github.com/remigermain/nested-multipart-parser/actions/workflows/main.yml)
44
[![pypi](https://img.shields.io/pypi/v/nested-multipart-parser)](https://pypi.org/project/nested-multipart-parser/)
55

6-
Parser for nested data for '*multipart/form*', you can use it any python project and you have a django rest framework integration.
6+
Parser for nested data for '*multipart/form*', you can use it in any python project, or use the Django Rest Framework integration.
77

8-
# Installation
8+
# Installation:
99

1010
```bash
1111
pip install nested-multipart-parser
1212
```
1313

14-
# How to use it
14+
# Usage:
1515

1616
```python
1717
from nested_multipart_parser import NestedParser
@@ -21,7 +21,7 @@ options = {
2121
}
2222

2323
def my_view():
24-
# options is optional
24+
# `options` is optional
2525
parser = NestedParser(data, options)
2626
if parser.is_valid():
2727
validate_data = parser.validate_data
@@ -31,7 +31,7 @@ def my_view():
3131

3232
```
3333

34-
### Django rest framwork
34+
### Django Rest Framework
3535

3636
```python
3737
from nested_multipart_parser.drf import DrfNestedParser
@@ -42,14 +42,14 @@ class YourViewSet(viewsets.ViewSet):
4242
```
4343

4444

45-
## What is doing
45+
## What it does:
4646

47-
The parser take the request data and transform to dictionary
47+
The parser take the request data and transform it to a Python dictionary:
4848

49-
exemple:
49+
example:
5050

5151
```python
52-
# input
52+
# input:
5353
{
5454
'title': 'title',
5555
'date': "time",
@@ -65,7 +65,7 @@ exemple:
6565
'langs[1][language]': "language1"
6666
}
6767

68-
# results are:
68+
# result:
6969
{
7070
'title': 'title',
7171
'date': "time",
@@ -92,114 +92,75 @@ exemple:
9292
}
9393
```
9494

95-
## How is work
95+
## How it works:
9696

97-
For this working perfectly you need to follow this rules:
98-
99-
- a first key need to be set ex: `title[0]` or `title`, in both the first key is `title`
100-
- each sub key need to be seperate by brackets `[ ]` or dot `.` (depends of your options)
101-
- if sub key are a full number, is converted to list *ex:* `[0]` or `[42]`
102-
- if sub key is Not a number is converted to dictionary *ex:* `[username]` or `[article]`
103-
- no space between separator
104-
- by default,the duplicate keys can't be set (see options to override that)
105-
ex:
97+
Attributes where sub keys are full numbers only are automatically converted into lists:
10698

10799
```python
108100
data = {
109-
'title[0]': 'my-value'``
101+
'title[0]': 'my-value',
102+
'title[1]': 'my-second-value'
110103
}
111-
# output
112104
output = {
113105
'title': [
114-
'my-value'
106+
'my-value',
107+
'my-second-value'
115108
]
116109
}
117110

118-
# invalid key
119-
data = {
120-
'title[688]': 'my-value'
121-
}
122-
# ERROR , you set a number is upper thans actual list
111+
# Be aware of the fact that you have to respect the order of the indices for arrays, thus
112+
'title[2]': 'my-value' # Invalid (you have to set title[0] and title[1] before)
123113

114+
# Also, you can't create an array on a key already set as a prinitive value (int, boolean or string):
115+
'title': 42,
116+
'title[object]': 42 # Invalid
117+
```
124118

125-
# wrong format if separator is brackets (see options)
126-
data = {
127-
'title[0]]]': 'my-value',
128-
'title[0': 'my-value',
129-
'title[': 'my-value',
130-
'title[]': 'my-value',
131-
'[]': 'my-value',
132-
}
133119

134-
data = {
135-
'title': 42,
136-
'title[object]': 42
137-
}
138-
# Error , title as alerady set by primitive value (int, boolean or string)
139120

140-
# many element in list
141-
data = {
142-
'title[0]': 'my-value',
143-
'title[1]': 'my-second-value'
144-
}
145-
# output
146-
output = {
147-
'title': [
148-
'my-value',
149-
'my-second-value'
150-
]
151-
}
121+
Attributes where sub keys are other than full numbers are converted into Python dictionary:
152122

153-
# converted to object
123+
```python
154124
data = {
155125
'title[key0]': 'my-value',
156126
'title[key7]': 'my-second-value'
157127
}
158-
# output
159128
output = {
160129
'title': {
161130
'key0': 'my-value',
162131
'key7': 'my-second-value'
163132
}
164133
}
165-
166-
# you have no limit for chained key
134+
135+
# You have no limit for chained key:
167136
data = {
168137
'the[0][chained][key][0][are][awesome][0][0]': 'im here !!'
169138
}
170-
# with "dot" separator in options is look like that
139+
# With "dot" separator option:
171140
data = {
172141
'the.0.chained.key.0.are.awesome.0.0': 'im here !!'
173142
}
174-
175-
# the output
176-
output: {
177-
'the': [
178-
{
179-
'chained':{
180-
'key': [
181-
{
182-
'are': {
183-
'awesome':
184-
[
185-
[
186-
'im here !!'
187-
]
188-
]
189-
}
190-
}
191-
]
192-
}
193-
}
194-
]
195-
}
196143
```
197144

145+
146+
147+
For this to work perfectly, you must follow the following rules:
148+
149+
- A first key always need to be set. ex: `title[0]` or `title`. In both cases the first key is `title`
150+
151+
- Each sub key need to be separate by brackets `[ ]` or dot `.` (depends of your options)
152+
153+
- Don't put spaces between separators.
154+
155+
- By default, you can't set set duplicates keys (see options)
156+
157+
158+
198159
## Options
199160

200161
```python
201162
{
202-
# the separator
163+
# Separators:
203164
# with bracket: article[title][authors][0]: "jhon doe"
204165
# with dot: article.title.authors.0: "jhon doe"
205166
'separator': 'bracket' or 'dot', # default is bracket
@@ -213,7 +174,7 @@ For this working perfectly you need to follow this rules:
213174
# }
214175
'raise_duplicate': True, # default is True
215176

216-
# overide the duplicate keys, you need to set "raise_duplicate" to False
177+
# override the duplicate keys, you need to set "raise_duplicate" to False
217178
# ex :
218179
# {
219180
# "article": 42,
@@ -230,7 +191,7 @@ For this working perfectly you need to follow this rules:
230191
}
231192
```
232193

233-
## Options with django rest framwork
194+
## Options for Django Rest Framwork:
234195
```python
235196

236197
# settings.py
@@ -244,10 +205,10 @@ DRF_NESTED_MULTIPART_PARSER = {
244205
}
245206
```
246207

247-
## Javscript integration
208+
## JavaScript integration:
248209

249-
You can use this [multipart-object](https://github.com/remigermain/multipart-object) library to easy convert object to flat nested object formated for this library
210+
You can use this [multipart-object](https://github.com/remigermain/multipart-object) library to easy convert object to flat nested object formatted for this library
250211

251212
## License
252213

253-
[MIT](https://github.com/remigermain/multipart-object/blob/main/LICENSE)
214+
[MIT](https://github.com/remigermain/multipart-object/blob/main/LICENSE)

0 commit comments

Comments
 (0)