helpers
check_required_config_parameters(new_model)
Verifies if ormar.Model has database and metadata set.
Recreates Connection pool for sqlite3
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_model |
Type[Model]
|
newly declared ormar Model |
required |
Source code in ormar\models\helpers\models.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
config_field_not_set(model, field_name)
Checks if field with given name is already present in model.OrmarConfig. Then check if it's set to something truthful (in practice meaning not None, as it's non or ormar Field only).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[Model]
|
newly constructed model |
required |
field_name |
str
|
name of the ormar field |
required |
Returns:
Type | Description |
---|---|
bool
|
result of the check |
Source code in ormar\models\helpers\models.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
expand_reverse_relationships(model)
Iterates through model_fields of given model and verifies if all reverse relation have been populated on related models.
If the reverse relation has not been set before it's set here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[Model]
|
model on which relation should be checked and registered |
required |
Source code in ormar\models\helpers\relations.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
extract_annotations_and_default_vals(attrs)
Extracts annotations from class namespace dict and triggers extraction of ormar model_fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attrs |
Dict
|
namespace of the class created |
required |
Returns:
Type | Description |
---|---|
Tuple[Dict, Dict]
|
namespace of the class updated, dict of extracted model_fields |
Source code in ormar\models\helpers\models.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
get_potential_fields(attrs)
Gets all the fields in current class namespace that are Fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attrs |
Union[Dict, MappingProxyType]
|
current class namespace |
required |
Returns:
Type | Description |
---|---|
Dict
|
extracted fields that are ormar Fields |
Source code in ormar\models\helpers\pydantic.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
get_pydantic_base_orm_config()
Returns empty pydantic Config with orm_mode set to True.
Returns:
Type | Description |
---|---|
pydantic Config
|
empty default config with orm_mode set. |
Source code in ormar\models\helpers\pydantic.py
98 99 100 101 102 103 104 105 106 |
|
merge_or_generate_pydantic_config(attrs, name)
Checks if the user provided pydantic Config, and if he did merges it with the default one.
Updates the attrs in place with a new config.
Source code in ormar\models\helpers\pydantic.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
modify_schema_example(model)
Modifies the schema example in openapi schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[Model]
|
newly constructed Model |
required |
Source code in ormar\models\helpers\validation.py
222 223 224 225 226 227 228 229 230 |
|
populate_config_sqlalchemy_table_if_required(config)
Constructs sqlalchemy table out of columns and parameters set on OrmarConfig. It populates name, metadata, columns and constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
OrmarConfig
|
OrmarConfig of the Model without sqlalchemy table constructed |
required |
Source code in ormar\models\helpers\sqlalchemy.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
populate_config_tablename_columns_and_pk(name, new_model)
Sets Model tablename if it's not already set in OrmarConfig. Default tablename if not present is class name lower + s (i.e. Bed becomes -> beds)
Checks if Model's OrmarConfig have pkname and columns set. If not calls the sqlalchemy_columns_from_model_fields to populate columns from ormar.fields definitions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the current Model |
required |
new_model |
Type[Model]
|
currently constructed Model |
required |
Returns:
Type | Description |
---|---|
ormar.models.metaclass.ModelMetaclass
|
Model with populated pkname and columns in OrmarConfig |
Raises:
Type | Description |
---|---|
ModelDefinitionError
|
if pkname is not present raises ModelDefinitionError. Each model has to have pk. |
Source code in ormar\models\helpers\sqlalchemy.py
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 250 251 252 253 254 255 256 257 |
|
populate_default_options_values(new_model, model_fields)
Sets all optional OrmarConfig values to its defaults and set model_fields that were already previously extracted.
Here should live all options that are not overwritten/set for all models.
Current options are: * constraints = [] * abstract = False
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_model |
Type[Model]
|
newly constructed Model |
required |
model_fields |
Dict
|
dict of model fields |
required |
Source code in ormar\models\helpers\models.py
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 |
|
register_relation_in_alias_manager(field)
Registers the relation (and reverse relation) in alias manager. The m2m relations require registration of through model between actual end models of the relation.
Delegates the actual registration to: m2m - register_many_to_many_relation_on_build fk - register_relation_on_build
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
ForeignKeyField
|
relation field |
required |
Source code in ormar\models\helpers\relations.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
remove_excluded_parent_fields(model)
Removes pydantic fields that should be excluded from parent models
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[Model]
|
|
required |
Source code in ormar\models\helpers\pydantic.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
sqlalchemy_columns_from_model_fields(model_fields, new_model)
Iterates over declared on Model model fields and extracts fields that should be treated as database fields.
If the model is empty it sets mandatory id field as primary key (used in through models in m2m relations).
Triggers a validation of relation_names in relation fields. If multiple fields are leading to the same related model only one can have empty related_name param. Also related_names have to be unique.
Trigger validation of primary_key - only one and required pk can be set
Sets owner
on each model_field as reference to newly created Model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_fields |
Dict
|
dictionary of declared ormar model fields |
required |
new_model |
Type[Model]
|
|
required |
Returns:
Type | Description |
---|---|
Tuple[Optional[str], List[sqlalchemy.Column]]
|
pkname, list of sqlalchemy columns |
Raises:
Type | Description |
---|---|
ModelDefinitionError
|
if validation of related_names fail, or pkname validation fails. |
Source code in ormar\models\helpers\sqlalchemy.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|