metaclass
ModelMetaclass
Bases: ModelMetaclass
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 | |
__getattr__(item)
Returns FieldAccessors on access to model fields from a class, that way it can be used in python style filters and order_by.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
str
|
name of the field |
required |
Returns:
| Type | Description |
|---|---|
FieldAccessor
|
FieldAccessor for given field |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 | |
__new__(mcs, name, bases, attrs, __pydantic_generic_metadata__=None, __pydantic_reset_parent_namespace__=True, _create_model_module=None, **kwargs)
Metaclass used by ormar Models that performs configuration and build of ormar Models.
Sets pydantic configuration. Extract model_fields and convert them to pydantic FieldInfo, updates class namespace.
Extracts settings and fields from parent classes. Fetches methods decorated with @computed_field decorator to expose them later in dict().
Construct parent pydantic Metaclass/ Model.
If class has ormar_config declared (so actual ormar Models) it also:
- populate sqlalchemy columns, primary key and tables from model_fields
- register reverse relationships on related models
- exposes alias manager on each Model
- creates QuerySet for each model and exposes it on a class
- sets custom serializers for relation models
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
name of current class |
required |
bases
|
Any
|
base classes |
required |
attrs
|
dict
|
class namespace |
required |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 | |
add_cached_properties(new_model)
Sets cached properties for both pydantic and ormar models.
Quick access fields are fields grabbed in getattribute to skip all checks.
Related fields and names are populated to None as they can change later. When children models are constructed they can modify parent to register itself.
All properties here are used as "cache" to not recalculate them constantly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_model
|
type[Model]
|
newly constructed Model |
required |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
add_field_descriptor(name, field, new_model)
Sets appropriate descriptor for each model field. There are 5 main types of descriptors, for bytes, json, pure pydantic fields, and 2 ormar ones - one for relation and one for pk shortcut
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
name of the field |
required |
field
|
BaseField
|
model field to add descriptor for |
required |
new_model
|
type[Model]
|
model with fields |
required |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
add_property_fields(new_model, attrs)
Checks class namespace for properties or functions with computed_field. If attribute have decorator_info it was decorated with @computed_field.
Functions like this are exposed in dict() (therefore also fastapi result). Names of property fields are cached for quicker access / extraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_model
|
type[Model]
|
newly constructed model |
required |
attrs
|
dict
|
|
required |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
copy_and_replace_m2m_through_model(field, field_name, table_name, parent_fields, attrs, ormar_config, base_class)
Clones class with Through model for m2m relations, appends child name to the name of the cloned class.
Clones non foreign keys fields from parent model, the same with database columns.
Modifies related_name with appending child table name after '_'
For table name, the table name of child is appended after '_'.
Removes the original sqlalchemy table from metadata if it was not removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_class
|
type[Model]
|
base class model |
required |
field
|
ManyToManyField
|
field with relations definition |
required |
field_name
|
str
|
name of the relation field |
required |
table_name
|
str
|
name of the table |
required |
parent_fields
|
dict
|
dictionary of fields to copy to new models from parent |
required |
attrs
|
dict
|
new namespace for class being constructed |
required |
ormar_config
|
OrmarConfig
|
metaclass of currently created model |
required |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
copy_data_from_parent_model(base_class, curr_class, attrs, model_fields)
Copy the key parameters [database, metadata, property_fields and constraints] and fields from parent models. Overwrites them if needed.
Only abstract classes can be subclassed.
Since relation fields requires different related_name for different children
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_class
|
type[Model]
|
one of the parent classes |
required |
curr_class
|
type
|
current constructed class |
required |
attrs
|
dict
|
new namespace for class being constructed |
required |
model_fields
|
dict[str, Union[BaseField, ForeignKeyField, ManyToManyField]]
|
ormar fields in defined in current class |
required |
Returns:
| Type | Description |
|---|---|
tuple[dict, dict]
|
updated attrs and model_fields |
Raises:
| Type | Description |
|---|---|
ModelDefinitionError
|
if non abstract model is subclassed |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
extract_from_parents_definition(base_class, curr_class, attrs, model_fields)
Extracts fields from base classes if they have valid ormar fields.
If model was already parsed -> fields definitions need to be removed from class cause pydantic complains about field re-definition so after first child we need to extract from parsed_fields not the class itself.
If the class is parsed first time annotations and field definition is parsed from the class.dict.
If the class is a ormar.Model it is skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_class
|
type
|
one of the parent classes |
required |
curr_class
|
type
|
current constructed class |
required |
attrs
|
dict
|
new namespace for class being constructed |
required |
model_fields
|
dict[str, Union[BaseField, ForeignKeyField, ManyToManyField]]
|
ormar fields in defined in current class |
required |
Returns:
| Type | Description |
|---|---|
tuple[dict, dict]
|
updated attrs and model_fields |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | |
get_constraint_copy(constraint)
Copy the constraint and unpacking it's values
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraint
|
ColumnCollectionConstraint
|
an instance of the ColumnCollectionConstraint class |
required |
Returns:
| Type | Description |
|---|---|
Union[UniqueColumns, IndexColumns, CheckColumns]
|
copy ColumnCollectionConstraint ormar constraints |
Raises:
| Type | Description |
|---|---|
ValueError
|
if non subclass of ColumnCollectionConstraint |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
register_signals(new_model)
Registers on model's SignalEmmiter and sets pre-defined signals. Predefined signals are (pre/post) + (save/update/delete).
Signals are emitted in both model own methods and in selected queryset ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_model
|
type[Model]
|
newly constructed model |
required |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
update_attrs_and_fields(attrs, new_attrs, model_fields, new_model_fields, new_fields)
Updates annotations, values of model fields (so pydantic FieldInfos) as well as model.ormar_config.model_fields definitions from parents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
dict
|
new namespace for class being constructed |
required |
new_attrs
|
dict
|
related of the namespace extracted from parent class |
required |
model_fields
|
dict
|
ormar fields in defined in current class |
required |
new_model_fields
|
dict
|
ormar fields defined in parent classes |
required |
new_fields
|
set
|
set of new fields names |
required |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
update_attrs_from_base_config(base_class, attrs, model_fields)
Updates OrmarConfig parameters in child from parent if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_class
|
Model
|
one of the parent classes |
required |
attrs
|
dict
|
new namespace for class being constructed |
required |
model_fields
|
dict
|
ormar fields in defined in current class |
required |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
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 | |
verify_constraint_names(base_class, model_fields, parent_value)
Verifies if redefined fields that are overwritten in subclasses did not remove any name of the column that is used in constraint as it will fail in sqlalchemy Table creation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_class
|
Model
|
one of the parent classes |
required |
model_fields
|
dict
|
ormar fields in defined in current class |
required |
parent_value
|
list
|
list of base class constraints |
required |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
wire_proxy_from_parent(new_model)
Resolve the concrete ormar parent of a proxy model and share its table, columns, primary key, model_fields, metadata and database with the proxy.
Proxy models do not own a SQLAlchemy table. They reuse the parent's table so that queries via the proxy class hit the same physical rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_model
|
type[Model]
|
the proxy model class being constructed |
required |
Raises:
| Type | Description |
|---|---|
ModelDefinitionError
|
if the proxy has no concrete ormar parent or if it inherits from multiple concrete ormar models with different tables. |
Source code in ormar/models/metaclass.py
| Python | |
|---|---|
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |