Commit 7a0bb22
authored
feature: saving data into model from ModelSchema (#24)
## Missing Tests
## Generic Type Support
```python
class UserSchema(ModelSchema[User]):
class Config:
model = User
include = ["id", "email", "profile"]
```
Inference type of Django model is supported in the schema class. This
allows for better IDE support and type checking. The `ModelSchema` class
can be used with any Django model, and the type of the model can be
specified as a generic type parameter.
inference in save method:
```python
from typing import TypeVar
from djantic import ModelSchema
from myapp.models import User
class UserSchema(ModelSchema[User]):
class Config:
model = User
include = ("username", "email", "first_name", "last_name", "is_staff")
serialized_user = UserSchema(
username="myusername",
email="my@email.com",
first_name="My First Name",
last_name="My Last Name",
is_staff=True,
)
new_user = serialized_user.save()
```
is optional, but it is recommended to use the `ModelSchema` class with
the Django model type as a generic type parameter. This allows for
better IDE support and type checking.
Also with generic type support, now it's not necessary to define `model`
in the `Config` class. The `ModelSchema` class will automatically infer
the model type from the generic type parameter. This allows to get model
type from the schema class itself.
```python
class UserSchema(ModelSchema[User]):
class Config:
include = ("username", "email", "first_name", "last_name", "is_staff")
serialized_user = UserSchema(
username="myusername",
email="my@email.com",
first_name="My First Name",
last_name="My Last Name",
is_staff=True,
)
new_user = serialized_user.save()
```1 parent 038500a commit 7a0bb22
File tree
5 files changed
+335
-19
lines changed- djantic
- docs
- tests
5 files changed
+335
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
24 | 27 | | |
| 28 | + | |
25 | 29 | | |
26 | 30 | | |
27 | 31 | | |
| 32 | + | |
| 33 | + | |
28 | 34 | | |
29 | 35 | | |
30 | 36 | | |
| |||
48 | 54 | | |
49 | 55 | | |
50 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
51 | 60 | | |
52 | 61 | | |
53 | 62 | | |
| 63 | + | |
54 | 64 | | |
55 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
56 | 81 | | |
57 | | - | |
58 | | - | |
| 82 | + | |
59 | 83 | | |
60 | 84 | | |
61 | 85 | | |
| |||
69 | 93 | | |
70 | 94 | | |
71 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
72 | 106 | | |
| 107 | + | |
73 | 108 | | |
74 | 109 | | |
75 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
76 | 121 | | |
77 | | - | |
| 122 | + | |
78 | 123 | | |
79 | 124 | | |
80 | 125 | | |
| |||
103 | 148 | | |
104 | 149 | | |
105 | 150 | | |
106 | | - | |
107 | 151 | | |
108 | 152 | | |
109 | 153 | | |
| |||
135 | 179 | | |
136 | 180 | | |
137 | 181 | | |
| 182 | + | |
138 | 183 | | |
139 | 184 | | |
140 | 185 | | |
| |||
143 | 188 | | |
144 | 189 | | |
145 | 190 | | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
150 | 195 | | |
151 | 196 | | |
152 | 197 | | |
| |||
157 | 202 | | |
158 | 203 | | |
159 | 204 | | |
160 | | - | |
| 205 | + | |
161 | 206 | | |
162 | 207 | | |
163 | 208 | | |
| |||
221 | 266 | | |
222 | 267 | | |
223 | 268 | | |
224 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
225 | 272 | | |
226 | 273 | | |
227 | 274 | | |
| |||
231 | 278 | | |
232 | 279 | | |
233 | 280 | | |
234 | | - | |
235 | | - | |
| 281 | + | |
236 | 282 | | |
237 | 283 | | |
238 | 284 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
456 | 456 | | |
457 | 457 | | |
458 | 458 | | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | | - | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
| 11 | + | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| |||
0 commit comments