@@ -4,22 +4,27 @@ title: Getting Started
44## Getting Started
55
66``` shell
7- poetry install pydantic-lazy
7+ poetry install pydantic-partials
88```
99
1010or
1111
1212``` shell
13- pip install pydantic-lazy
13+ pip install pydantic-partials
1414```
1515
16- Very basic example:
16+ By default, all fields without a default value will have the ability to be partial,
17+ and can be missing from both validation and serialization.
18+
19+ Very basic example is below:
1720
1821``` python
19- from pydantic_lazy import LazyModel, Missing, Partial
22+ from pydantic_partials import PartialModel, Missing, Partial
23+
24+
25+ class MyModel (PartialModel ):
26+ some_attr: str
2027
21- class MyModel (LazyModel ):
22- some_attr: Partial[str ]
2328
2429# By default, Partial fields without any value will get set to a special `Missing` type.
2530# Any field that is set to Missing is excluded from the model_dump/model_dump_json
@@ -34,29 +39,66 @@ assert obj.model_dump() == {'some_attr': 'hello'}
3439
3540# You can always manually set a field to `Missing` directly.
3641obj.some_attr = Missing
42+
43+ # And now it's removed from the model-dump.
3744assert obj.model_dump() == {}
45+
46+ # The json dump is also affected in the same way.
47+ assert obj.model_dump_json() == ' {} '
48+ ```
49+
50+
51+ You can turn off this default behavior by via ` auto_partials ` class argument or modeL_config option:
52+
53+ ``` python
54+ from pydantic_partials import PartialModel, PartialConfigDict
55+
56+ class TestModel1 (PartialModel , auto_partials = False ):
57+ ...
58+
59+ class TestModel2 (PartialModel ):
60+ model_config = PartialConfigDict(auto_partials = False )
61+ ...
62+
3863```
3964
65+ You can disable this automatic function. This means you have complete control of exactly which field
66+ can be partial or not. You can use either the generic ` Partial[...] ` generic or a union with ` MissingType `
67+ to mark a field as a partial field. The generic simple makes the union to MissingType for you.
4068
41- ## Lazily Fetch Fields
69+ Example of disabling auto_partials:
4270
4371``` python
44- from typing import Any
45- from pydantic_lazy import LazyModel, Missing, Partial
46-
47- class MyModel (LazyModel ):
48- def model_resolve_missing (self , field_name : str ) -> Any:
49- # ... Lookup the `field_name` and return its value
50- if field_name == ' some_attr' :
51- return " fetched"
52- # You can also return Missing to indicate that it's still missing/unfetchable.
53- return Missing
54- some_attr: Partial[str ]
55- another_attr: Partial[int ]
56-
57- # Resolve function called whenever a Missing value would otherwise be returned.
58- assert MyModel().some_attr == ' fetched'
59- assert MyModel().another_attr is Missing
60- assert MyModel(some_attr = ' other-value' ).some_attr == ' other-value'
72+ from pydantic_partials import PartialModel, Missing, MissingType, Partial, PartialConfigDict
73+ from decimal import Decimal
74+ from pydantic import ValidationError
75+
76+ class TestModel (PartialModel , auto_partials = False ):
77+ # Can use `Partial` generic type
78+ partial_int: Partial[int ] = Missing
79+
80+ # Or union with `MissingType`
81+ partial_str: str | MissingType
82+
83+ required_decimal: Decimal
84+
85+ try :
86+ TestModel()
87+ except ValidationError as e:
88+ print (f ' Pydantic will state `required_decimal` is required: { e} ' )
89+ else :
90+ raise Exception (' Pydantic should have required `required_decimal`.' )
91+
92+ obj = TestModel(required_decimal = ' 1.34' )
93+
94+ # You can find out at any time if a field is missing or not:
95+ assert obj.partial_int is Missing
96+ assert obj.partial_str is Missing
97+
98+ assert obj.required_decimal == Decimal(' 1.34' )
6199
62100```
101+
102+
103+
104+
0 commit comments