@@ -18,44 +18,51 @@ Full documentation for the project is available at [https://nxexox.github.io/pyt
1818
1919Install using ` pip ` , including any optional packages you want...
2020
21- pip install python-rest-framework
21+ ``` bash
22+ pip install python-rest-framework
23+ ```
2224
2325...or clone the project from github.
2426
25- git clone [email protected] :nxexox/python-rest-framework.git 26-
27+ ``` bash
28+ git clone
[email protected] :nxexox/python-rest-framework.git
29+ ```
2730
2831## Example
2932
3033For example, we will serialize the data from the request object.
3134
3235First we write the serializer
3336
34- from rest_framework.serializers import (
35- Serializer, CharField, IntegerField, ListField, FloatField
36- )
37+ ``` python
38+ from rest_framework.serializers import (
39+ Serializer, CharField, IntegerField, ListField, FloatField
40+ )
3741
38- # Example serializer for parsing body data from web request.
39- class ExampleSerializer(Serializer):
40- char_field = CharField(label='This char field', required=True)
41- int_field = IntegerField(label='This int field', required=True)
42- list_float_field = ListField(child=FloatField(), required=True, min_length=2)
42+ # Example serializer for parsing body data from web request.
43+ class ExampleSerializer (Serializer ):
44+ char_field = CharField(label = ' This char field' , required = True )
45+ int_field = IntegerField(label = ' This int field' , required = True )
46+ list_float_field = ListField(child = FloatField(), required = True , min_length = 2 )
47+ ```
4348
4449---
4550
4651Now we process the request body with a serializer
4752
48- # web request data
49- data = {
50- 'char_field': 'example', 'int_field': 1,
51- 'list_float_field': [1.0, 1.1, 1.2]
52- }
53-
54- ser = ExampleSerializer(data=data)
55- if ser.is_valid():
56- print(ser.validated_data)
57- else:
58- print(ser.errors)
53+ ``` python
54+ # web request data
55+ data = {
56+ ' char_field' : ' example' , ' int_field' : 1 ,
57+ ' list_float_field' : [1.0 , 1.1 , 1.2 ]
58+ }
59+
60+ ser = ExampleSerializer(data = data)
61+ if ser.is_valid():
62+ print (ser.validated_data)
63+ else :
64+ print (ser.errors)
65+ ```
5966
6067[ docs ] : https://nxexox.github.io/python-rest-framework/
6168[ pypi-version ] : https://img.shields.io/pypi/v/python-rest-framework.svg
0 commit comments