30
30
data serialization and validation.
31
31
32
32
[ Django REST framework] ( https://www.django-rest-framework.org ) is a framework built
33
- on top of [ Django] ( https://www.djangoproject.com/ ) which allows writing REST APIs.
33
+ on top of [ Django] ( https://www.djangoproject.com/ ) used to write REST APIs.
34
34
35
- If like me you develop DRF APIs and you like pydantic , ` drf-pydantic ` is for you 😍.
35
+ If you develop DRF APIs and rely on pydantic for data validation/(de)serialization ,
36
+ then ` drf-pydantic ` is for you 😍.
36
37
37
38
# Installation
38
39
@@ -55,23 +56,36 @@ class MyModel(BaseModel):
55
56
addresses: list[str ]
56
57
```
57
58
59
+ ` MyModel.drf_serializer ` would be equvalent to the following DRF Serializer class:
60
+
61
+ ``` python
62
+ class MyModelSerializer :
63
+ name = CharField(allow_null = False , required = True )
64
+ addresses = ListField(
65
+ allow_empty = True ,
66
+ allow_null = False ,
67
+ child = CharField(allow_null = False ),
68
+ required = True ,
69
+ )
70
+ ```
71
+
58
72
Whenever you need a DRF serializer you can get it from the model like this:
59
73
60
74
``` python
61
- MyModel.drf_serializer
75
+ my_value = MyModel.drf_serializer(data = {" name" : " Van" , addresses: [" Gym" ]})
76
+ my_value.is_valid(raise_exception = True )
62
77
```
63
78
64
79
> ℹ️ ** INFO** <br >
65
80
> Models created using ` drf_pydantic ` are fully idenditcal to those created by
66
- > ` pydantic ` . The only change is the addition of the ` drf_serializer ` attribute
67
- > during class creation (not instance).
81
+ > ` pydantic ` . The only change is the addition of the ` drf_serializer ` attribute.
68
82
69
83
## Existing Models
70
84
71
- If you have an existing code base and you would like to use the ` drf_serializer `
72
- attribute to only specific models, then great news 🥳 - you can easily extend
73
- your existign ` pydantic ` models by adding ` drf_pydantic.BaseModel ` to the list
74
- of parent classes.
85
+ If you have an existing code base and you would like to add the ` drf_serializer `
86
+ attribute only to some of your models, then I have great news 🥳 - you can easily
87
+ extend your existing ` pydantic ` models by adding ` drf_pydantic.BaseModel ` to the list
88
+ of parent classes of the model you want to extend .
75
89
76
90
Your existing pydantic models:
77
91
@@ -108,7 +122,7 @@ Dog.drf_serializer
108
122
109
123
If you have nested models and you want to generate serializer only from one of them,
110
124
you don't have to update all models - only update the model you need, ` drf_pydantic `
111
- will generate serializers for all normal nested ` pydantic ` models for free 🐱👤 .
125
+ will generate serializers for all normal nested ` pydantic ` models for free 🥷 .
112
126
113
127
``` python
114
128
from drf_pydantic import BaseModel as DRFBaseModel
@@ -132,4 +146,3 @@ Block.drf_serializer
132
146
133
147
- Add support for custom field types (both for pydantic and DRF)
134
148
- Add option to create custom serializer for complex models
135
- - Add support for constraints (max, min, regex, etc.)
0 commit comments