rebuild
Deferred rebuilds of pydantic schemas of ormar models.
Registering a reverse relation (or a through model field) adds a field to a model that pydantic has already built. Instead of rebuilding such a model right away, once per added field, it is marked as pending and rebuilt once, with its final fields, when its schema, validator or serializer is first needed.
Until then other ormar models built in the meantime embed the schema the pending model had before it was marked, the same snapshot a forced rebuild used to leave behind, so nested relation shapes stay the same.
building_schemas()
Marks that ormar is building pydantic schemas of its own models, so pending models embedded in them are not rebuilt but reused as they are.
Returns:
| Type | Description |
|---|---|
Iterator[None]
|
context manager |
Source code in ormar/models/helpers/rebuild.py
| Python | |
|---|---|
37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
connected_models(model)
Returns the model and every model reachable from it through relations.
Reverse relations are registered on both sides, so this is the set of models whose schemas can embed each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model]
|
model to start from |
required |
Returns:
| Type | Description |
|---|---|
list[Model class]
|
connected model classes, starting with the given model |
Source code in ormar/models/helpers/rebuild.py
| Python | |
|---|---|
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
mark_for_rebuild(model)
Marks the model as needing a pydantic rebuild instead of rebuilding it.
Schema, validator and serializer of the model are replaced with mocks that rebuild all pending connected models on first access. The previous schema is kept as a snapshot for schemas ormar builds until then.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model]
|
model whose fields changed |
required |
Source code in ormar/models/helpers/rebuild.py
| Python | |
|---|---|
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
rebuild_model(model)
Rebuilds pydantic schema, validator and serializer of the model with its current fields and clears its pending rebuild mark.
If the rebuild fails (i.e. a related model still has an unresolved ForwardRef) the pending mark, snapshot and rebuild mocks are restored, so the rebuild is retried once the reference is resolved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model]
|
model to rebuild |
required |
Source code in ormar/models/helpers/rebuild.py
| Python | |
|---|---|
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
rebuild_pending_models(model)
Rebuilds every pending model connected to the model, each one once with its final fields, in the order they were last marked.
That is the order in which eager rebuilds finished the models, so models rebuilt later embed the final schemas of the ones rebuilt before them and nested shapes stay close to the ones eager rebuilds produced.
While ormar builds its own schemas only the given model is rebuilt (if pending), the other ones are embedded as snapshots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model]
|
model whose schema is needed |
required |
Source code in ormar/models/helpers/rebuild.py
| Python | |
|---|---|
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
related_models(model)
Returns models directly linked to the model by its relation fields, including through models of many to many relations.
Relations still pointing to an unresolved ForwardRef are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model]
|
model whose relations are collected |
required |
Returns:
| Type | Description |
|---|---|
list[Model class]
|
related model classes |
Source code in ormar/models/helpers/rebuild.py
| Python | |
|---|---|
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
schema_snapshot(model)
Returns the schema the model had before it was marked as pending, used while ormar builds schemas of other models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model]
|
model to check |
required |
Returns:
| Type | Description |
|---|---|
Optional[CoreSchema]
|
schema snapshot or None if model is not pending or had none |
Source code in ormar/models/helpers/rebuild.py
| Python | |
|---|---|
169 170 171 172 173 174 175 176 177 178 179 180 181 | |
set_rebuild_mocks(model)
Replaces pydantic schema, validator and serializer of the model with mocks that rebuild pending models on first access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model]
|
model to mock |
required |
Source code in ormar/models/helpers/rebuild.py
| Python | |
|---|---|
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |