@@ -19,7 +19,7 @@ and can be missing from both validation and serialization.
1919Very basic example is below:
2020
2121``` python
22- from pydantic_partials import PartialModel, Missing, Partial
22+ from pydantic_partials import PartialModel, Missing
2323
2424
2525class MyModel (PartialModel ):
@@ -51,10 +51,8 @@ obj.another_field = 'assigned-value'
5151
5252# And now it's removed from the model-dump.
5353assert obj.model_dump() == {' another_field' : ' assigned-value' }
54-
5554```
5655
57-
5856You can turn off this default behavior by via ` auto_partials ` class argument or modeL_config option:
5957
6058``` python
@@ -66,7 +64,6 @@ class TestModel1(PartialModel, auto_partials=False):
6664class TestModel2 (PartialModel ):
6765 model_config = PartialConfigDict(auto_partials = False )
6866 ...
69-
7067```
7168
7269You can disable this automatic function. This means you have complete control of exactly which field
@@ -76,7 +73,7 @@ to mark a field as a partial field. The generic simple makes the union to Missi
7673Example of disabling auto_partials:
7774
7875``` python
79- from pydantic_partials import PartialModel, Missing, MissingType, Partial, PartialConfigDict
76+ from pydantic_partials import PartialModel, Missing, MissingType, Partial
8077from decimal import Decimal
8178from pydantic import ValidationError
8279
@@ -103,9 +100,43 @@ assert obj.partial_int is Missing
103100assert obj.partial_str is Missing
104101
105102assert obj.required_decimal == Decimal(' 1.34' )
106-
107103```
108104
109105
110106
107+ You can inherit from a model to make a partial-version of the inherited fields:
108+
109+ ``` python
110+ from pydantic_partials import PartialModel, Missing
111+ from pydantic import ValidationError, BaseModel
112+
113+
114+ class TestModel (BaseModel ):
115+ name: str
116+ value: str
117+ some_null_by_default_field: str | None = None
118+
119+
120+ try :
121+ TestModel()
122+ except ValidationError as e:
123+ print (f ' Pydantic will state `name` + `value` are required: { e} ' )
124+ else :
125+ raise Exception (' Pydantic should have required `required_decimal`.' )
126+
127+
128+ class PartialTestModel (PartialModel , TestModel ):
129+ pass
130+
131+
132+ obj = PartialTestModel(name = ' a-name' )
133+
134+ assert obj.name == ' a-name'
135+ assert obj.value is Missing
136+ assert obj.some_null_by_default_field is None
137+ ```
138+
139+ Notice that if a field has a default value, it's used instead of marking it as ` Missing ` .
111140
141+ Also, the ` Missing ` sentinel value is a separate value vs ` None ` , allowing one to easily
142+ know if a value is truly just missing or is ` None ` /` Null ` .
0 commit comments