ormar
The ormar
package is an async mini ORM for Python, with support for Postgres,
MySQL, and SQLite.
The main benefit of using ormar
are:
- getting an async ORM that can be used with async frameworks (fastapi, starlette etc.)
- getting just one model to maintain - you don't have to maintain pydantic and other orm model (sqlalchemy, peewee, gino etc.)
The goal was to create a simple ORM that can be used directly
(as request and response models)
with fastapi
that bases it's data validation on pydantic.
Ormar - apart form obvious ORM in name - get it's name from ormar in swedish which means snakes, and ormar(e) in italian which means cabinet.
And what's a better name for python ORM than snakes cabinet :)
BaseField
Bases: FieldInfo
BaseField serves as a parent class for all basic Fields in ormar. It keeps all common parameters available for all fields as well as set of useful functions.
All values are kept as class variables, ormar Fields are never instantiated. Subclasses pydantic.FieldInfo to keep the fields related to pydantic field types like ConstrainedStr
Source code in ormar/fields/base.py
18 19 20 21 22 23 24 25 26 27 28 29 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 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 88 89 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 116 117 118 119 120 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 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 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 352 353 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 |
|
construct_constraints()
Converts list of ormar constraints into sqlalchemy ForeignKeys. Has to be done dynamically as sqlalchemy binds ForeignKey to the table. And we need a new ForeignKey for subclasses of current model
:return: List of sqlalchemy foreign keys - by default one. :rtype: List[sqlalchemy.schema.ForeignKey]
Source code in ormar/fields/base.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
default_value(use_server=False)
Returns a FieldInfo instance with populated default (static) or default_factory (function). If the field is a autoincrement primary key the default is None. Otherwise field have to has either default, or default_factory populated.
If all default conditions fail None is returned.
Used in converting to pydantic FieldInfo.
:param use_server: flag marking if server_default should be treated as default value, default False :type use_server: bool :return: returns a call to pydantic.Field which is returning a FieldInfo instance :rtype: Optional[pydantic.FieldInfo]
Source code in ormar/fields/base.py
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 167 168 |
|
evaluate_forward_ref(globalns, localns)
Evaluates the ForwardRef to actual Field based on global and local namespaces
:param globalns: global namespace :type globalns: Any :param localns: local namespace :type localns: Any :return: None :rtype: None
Source code in ormar/fields/base.py
360 361 362 363 364 365 366 367 368 369 370 |
|
expand_relationship(value, child, to_register=True)
Function overwritten for relations, in basic field the value is returned as is. For relations the child model is first constructed (if needed), registered in relation and returned. For relation fields the value can be a pk value (Any type of field), dict (from Model) or actual instance/list of a "Model".
:param value: a Model field value, returned untouched for non relation fields. :type value: Any :param child: a child Model to register :type child: Union["Model", "NewBaseModel"] :param to_register: flag if the relation should be set in RelationshipManager :type to_register: bool :return: returns untouched value for normal fields, expands only for relations :rtype: Any
Source code in ormar/fields/base.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
get_alias()
Used to translate Model column names to database column names during db queries.
:return: returns custom database column name if defined by user, otherwise field name in ormar/pydantic :rtype: str
Source code in ormar/fields/base.py
115 116 117 118 119 120 121 122 123 |
|
get_column(name)
Returns definition of sqlalchemy.Column used in creation of sqlalchemy.Table. Populates name, column type constraints, as well as a number of parameters like primary_key, index, unique, nullable, default and server_default.
:param name: name of the db column - used if alias is not set :type name: str :return: actual definition of the database column as sqlalchemy requires. :rtype: sqlalchemy.Column
Source code in ormar/fields/base.py
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 |
|
get_default(use_server=False, call_default_factory=True)
Return default value for a field. If the field is Callable the function is called and actual result is returned. Used to populate default_values for pydantic Model in ormar Model Metaclass.
:param use_server: flag marking if server_default should be treated as default value, default False :type use_server: bool :return: default value for the field if set, otherwise implicit None :rtype: Any
Source code in ormar/fields/base.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
get_pydantic_default()
Generates base pydantic.FieldInfo with only default and optionally required to fix pydantic Json field being set to required=False. Used in an ormar Model Metaclass.
:return: instance of base pydantic.FieldInfo :rtype: pydantic.FieldInfo
Source code in ormar/fields/base.py
125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
get_related_name()
Returns name to use for reverse relation.
It's either set as related_name
or by default it's owner model. get_name + 's'
:return: name of the related_name or default related name.
:rtype: str
Source code in ormar/fields/base.py
372 373 374 375 376 377 378 379 |
|
has_default(use_server=True)
Checks if the field has default value set.
:param use_server: flag marking if server_default should be treated as default value, default False :type use_server: bool :return: result of the check if default value is set :rtype: bool
Source code in ormar/fields/base.py
209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
has_unresolved_forward_refs()
Verifies if the filed has any ForwardRefs that require updating before the model can be used.
:return: result of the check :rtype: bool
Source code in ormar/fields/base.py
350 351 352 353 354 355 356 357 358 |
|
is_auto_primary_key()
Checks if field is first a primary key and if it, it's than check if it's set to autoincrement. Autoincrement primary_key is nullable/optional.
:return: result of the check for primary key and autoincrement :rtype: bool
Source code in ormar/fields/base.py
223 224 225 226 227 228 229 230 231 232 233 234 |
|
is_valid_uni_relation()
Checks if field is a relation definition but only for ForeignKey relation, so excludes ManyToMany fields, as well as virtual ForeignKey (second side of FK relation).
Is used to define if a field is a db ForeignKey column that should be saved/populated when dealing with internal/own Model columns only.
:return: result of the check :rtype: bool
Source code in ormar/fields/base.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
set_self_reference_flag()
Sets self_reference
to True if field to and owner are same model.
:return: None
:rtype: None
Source code in ormar/fields/base.py
338 339 340 341 342 343 344 345 346 347 348 |
|
BigInteger
Bases: Integer
, int
BigInteger field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
579 580 581 582 583 584 585 586 587 588 589 590 |
|
Boolean
Bases: ModelFieldFactory
, int
Boolean field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
340 341 342 343 344 345 346 347 348 349 350 351 |
|
CheckColumns
Bases: CheckConstraint
Subclass of sqlalchemy.CheckConstraint. Used to avoid importing anything from sqlalchemy by user.
Note that some databases do not actively support check constraints such as MySQL.
Source code in ormar/fields/constraints.py
25 26 27 28 29 30 31 |
|
Date
Bases: ModelFieldFactory
, date
Date field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
397 398 399 400 401 402 403 404 405 406 407 408 |
|
DateTime
Bases: ModelFieldFactory
, datetime
DateTime field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
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 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
375 376 377 378 379 380 381 382 383 384 385 386 |
|
Decimal
Bases: ModelFieldFactory
, Decimal
Decimal field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 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 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
684 685 686 687 688 689 690 691 692 693 694 695 696 697 |
|
validate(**kwargs)
classmethod
Used to validate if all required parameters on a given field type are set. :param kwargs: all params passed during construction :type kwargs: Any
Source code in ormar/fields/model_fields.py
699 700 701 702 703 704 705 706 707 708 709 710 711 |
|
Enum
Bases: ModelFieldFactory
Enum field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
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 |
|
ExcludableItems
Keeps a dictionary of Excludables by alias + model_name keys to allow quick lookup by nested models without need to travers deeply nested dictionaries and passing include/exclude around
Source code in ormar/models/excludable.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 116 117 118 119 120 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 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 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 |
|
build(items, model_cls, is_exclude=False)
Receives the one of the types of items and parses them as to achieve a end situation with one excludable per alias/model in relation.
Each excludable has two sets of values - one to include, one to exclude.
:param items: values to be included or excluded :type items: Union[List[str], str, Tuple[str], Set[str], Dict] :param model_cls: source model from which relations are constructed :type model_cls: ormar.models.metaclass.ModelMetaclass :param is_exclude: flag if items should be included or excluded :type is_exclude: bool
Source code in ormar/models/excludable.py
117 118 119 120 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 156 157 158 159 |
|
from_excludable(other)
classmethod
Copy passed ExcludableItems to avoid inplace modifications.
:param other: other excludable items to be copied :type other: ormar.models.excludable.ExcludableItems :return: copy of other :rtype: ormar.models.excludable.ExcludableItems
Source code in ormar/models/excludable.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
get(model_cls, alias='')
Return Excludable for given model and alias.
:param model_cls: target model to check :type model_cls: ormar.models.metaclass.ModelMetaclass :param alias: table alias from relation manager :type alias: str :return: Excludable for given model and alias :rtype: ormar.models.excludable.Excludable
Source code in ormar/models/excludable.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
include_entry_count()
Returns count of include items inside
Source code in ormar/models/excludable.py
90 91 92 93 94 95 96 97 |
|
Float
Bases: ModelFieldFactory
, float
Float field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
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 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
311 312 313 314 315 316 317 318 319 320 321 322 |
|
ForeignKeyField
Bases: BaseField
Actual class returned from ForeignKey function call and stored in model_fields.
Source code in ormar/fields/foreign_key.py
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 352 353 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 461 462 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 540 541 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 570 571 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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
|
default_source_field_name()
Returns default target model name on through model. :return: name of the field :rtype: str
Source code in ormar/fields/foreign_key.py
360 361 362 363 364 365 366 367 |
|
default_target_field_name()
Returns default target model name on through model. :return: name of the field :rtype: str
Source code in ormar/fields/foreign_key.py
351 352 353 354 355 356 357 358 |
|
evaluate_forward_ref(globalns, localns)
Evaluates the ForwardRef to actual Field based on global and local namespaces
:param globalns: global namespace :type globalns: Any :param localns: local namespace :type localns: Any :return: None :rtype: None
Source code in ormar/fields/foreign_key.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
|
expand_relationship(value, child, to_register=True)
For relations the child model is first constructed (if needed), registered in relation and returned. For relation fields the value can be a pk value (Any type of field), dict (from Model) or actual instance/list of a "Model".
Selects the appropriate constructor based on a passed value.
:param value: a Model field value, returned untouched for non relation fields. :type value: Any :param child: a child Model to register :type child: Union["Model", "NewBaseModel"] :param to_register: flag if the relation should be set in RelationshipManager :type to_register: bool :return: returns a Model or a list of Models :rtype: Optional[Union["Model", List["Model"]]]
Source code in ormar/fields/foreign_key.py
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
|
get_model_relation_fields(use_alias=False)
Extract names of the database columns or model fields that are connected with given relation based on use_alias switch and which side of the relation the current field is - reverse or normal.
:param use_alias: use db names aliases or model fields :type use_alias: bool :return: name or names of the related columns/ fields :rtype: Union[str, List[str]]
Source code in ormar/fields/foreign_key.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
|
get_related_field_alias()
Extract names of the related database columns or that are connected with given relation based to use as a target in filter clause.
:return: name or names of the related columns/ fields :rtype: Union[str, Dict[str, str]]
Source code in ormar/fields/foreign_key.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
get_related_field_name()
Returns name of the relation field that should be used in prefetch query. This field is later used to register relation in prefetch query, populate relations dict, and populate nested model in prefetch query.
:return: name(s) of the field :rtype: Union[str, List[str]]
Source code in ormar/fields/foreign_key.py
414 415 416 417 418 419 420 421 422 423 424 425 |
|
get_related_name()
Returns name to use for reverse relation.
It's either set as related_name
or by default it's owner model. get_name + 's'
:return: name of the related_name or default related name.
:rtype: str
Source code in ormar/fields/foreign_key.py
342 343 344 345 346 347 348 349 |
|
get_relation_name()
Returns name of the relation, which can be a own name or through model names for m2m models
:return: result of the check :rtype: bool
Source code in ormar/fields/foreign_key.py
640 641 642 643 644 645 646 647 648 |
|
get_source_model()
Returns model from which the relation comes -> either owner or through model
:return: source model :rtype: Type["Model"]
Source code in ormar/fields/foreign_key.py
650 651 652 653 654 655 656 657 |
|
get_source_related_name()
Returns name to use for source relation name.
For FK it's the same, differs for m2m fields.
It's either set as related_name
or by default it's owner model. get_name + 's'
:return: name of the related_name or default related name.
:rtype: str
Source code in ormar/fields/foreign_key.py
332 333 334 335 336 337 338 339 340 |
|
has_unresolved_forward_refs()
Verifies if the filed has any ForwardRefs that require updating before the model can be used.
:return: result of the check :rtype: bool
Source code in ormar/fields/foreign_key.py
594 595 596 597 598 599 600 601 602 |
|
register_relation(model, child)
Registers relation between parent and child in relation manager. Relation manager is kep on each model (different instance).
Used in Metaclass and sometimes some relations are missing (i.e. cloned Models in fastapi might miss one).
:param model: parent model (with relation definition) :type model: Model class :param child: child model :type child: Model class
Source code in ormar/fields/foreign_key.py
579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
|
Integer
Bases: ModelFieldFactory
, int
Integer field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
202 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 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
236 237 238 239 240 241 242 243 244 245 246 247 |
|
JSON
Bases: ModelFieldFactory
, Json
JSON field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
454 455 456 457 458 459 460 461 462 463 464 465 |
|
LargeBinary
Bases: ModelFieldFactory
, bytes
LargeBinary field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
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 540 541 542 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
518 519 520 521 522 523 524 525 526 527 528 529 |
|
validate(**kwargs)
classmethod
Used to validate if all required parameters on a given field type are set. :param kwargs: all params passed during construction :type kwargs: Any
Source code in ormar/fields/model_fields.py
531 532 533 534 535 536 537 538 539 540 541 542 |
|
ManyToManyField
Bases: ForeignKeyField
, QuerySetProtocol
, RelationProtocol
Actual class returned from ManyToMany function call and stored in model_fields.
Source code in ormar/fields/many_to_many.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 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 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 |
|
create_default_through_model()
Creates default empty through model if no additional fields are required.
Source code in ormar/fields/many_to_many.py
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 |
|
evaluate_forward_ref(globalns, localns)
Evaluates the ForwardRef to actual Field based on global and local namespaces
:param globalns: global namespace :type globalns: Any :param localns: local namespace :type localns: Any :return: None :rtype: None
Source code in ormar/fields/many_to_many.py
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 |
|
get_model_relation_fields(use_alias=False)
Extract names of the database columns or model fields that are connected with given relation based on use_alias switch.
:param use_alias: use db names aliases or model fields :type use_alias: bool :return: name or names of the related columns/ fields :rtype: Union[str, List[str]]
Source code in ormar/fields/many_to_many.py
274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
get_related_field_alias()
Extract names of the related database columns or that are connected with given relation based to use as a target in filter clause.
:return: name or names of the related columns/ fields :rtype: Union[str, Dict[str, str]]
Source code in ormar/fields/many_to_many.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|
get_related_field_name()
Returns name of the relation field that should be used in prefetch query. This field is later used to register relation in prefetch query, populate relations dict, and populate nested model in prefetch query.
:return: name(s) of the field :rtype: Union[str, List[str]]
Source code in ormar/fields/many_to_many.py
303 304 305 306 307 308 309 310 311 312 313 314 |
|
get_relation_name()
Returns name of the relation, which can be a own name or through model names for m2m models
:return: result of the check :rtype: bool
Source code in ormar/fields/many_to_many.py
250 251 252 253 254 255 256 257 258 259 260 |
|
get_source_model()
Returns model from which the relation comes -> either owner or through model
:return: source model :rtype: Type["Model"]
Source code in ormar/fields/many_to_many.py
262 263 264 265 266 267 268 269 |
|
get_source_related_name()
Returns name to use for source relation name.
For FK it's the same, differs for m2m fields.
It's either set as related_name
or by default it's field name.
:return: name of the related_name or default related name.
:rtype: str
Source code in ormar/fields/many_to_many.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
has_unresolved_forward_refs()
Verifies if the filed has any ForwardRefs that require updating before the model can be used.
:return: result of the check :rtype: bool
Source code in ormar/fields/many_to_many.py
213 214 215 216 217 218 219 220 221 |
|
Model
Bases: ModelRow
Source code in ormar/models/model.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 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 88 89 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 116 117 118 119 120 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 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 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 |
|
delete()
async
Removes the Model instance from the database.
Sends pre_delete and post_delete signals.
Sets model save status to False.
Note it does not delete the Model itself (python object). So you can delete and later save (since pk is deleted no conflict will arise) or update and the Model will be saved in database again.
:return: number of deleted rows (for some backends) :rtype: int
Source code in ormar/models/model.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
|
load()
async
Allow to refresh existing Models fields from database. Be careful as the related models can be overwritten by pk_only models in load. Does NOT refresh the related models fields if they were loaded before.
:raises NoMatch: If given pk is not found in database.
:return: reloaded Model :rtype: Model
Source code in ormar/models/model.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|
load_all(follow=False, exclude=None, order_by=None)
async
Allow to refresh existing Models fields from database. Performs refresh of the related models fields.
By default, loads only self and the directly related ones.
If follow=True is set it loads also related models of related models.
To not get stuck in an infinite loop as related models also keep a relation to parent model visited models set is kept.
That way already visited models that are nested are loaded, but the load do not follow them inside. So Model A -> Model B -> Model C -> Model A -> Model X will load second Model A but will never follow into Model X. Nested relations of those kind need to be loaded manually.
:param order_by: columns by which models should be sorted :type order_by: Union[List, str] :raises NoMatch: If given pk is not found in database.
:param exclude: related models to exclude :type exclude: Union[List, str, Set, Dict] :param follow: flag to trigger deep save - by default only directly related models are saved with follow=True also related models of related models are saved :type follow: bool :return: reloaded Model :rtype: Model
Source code in ormar/models/model.py
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 |
|
save()
async
Performs a save of given Model instance. If primary key is already saved, db backend will throw integrity error.
Related models are saved by pk number, reverse relation and many to many fields are not saved - use corresponding relations methods.
If there are fields with server_default set and those fields are not already filled save will trigger also a second query to refreshed the fields populated server side.
Does not recognize if model was previously saved. If you want to perform update or insert depending on the pk fields presence use upsert.
Sends pre_save and post_save signals.
Sets model save status to True.
:return: saved Model :rtype: Model
Source code in ormar/models/model.py
53 54 55 56 57 58 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
save_related(follow=False, save_all=False, relation_map=None, exclude=None, update_count=0, previous_model=None, relation_field=None)
async
Triggers a upsert method on all related models if the instances are not already saved. By default saves only the directly related ones.
If follow=True is set it saves also related models of related models.
To not get stuck in an infinite loop as related models also keep a relation to parent model visited models set is kept.
That way already visited models that are nested are saved, but the save do not follow them inside. So Model A -> Model B -> Model A -> Model C will save second Model A but will never follow into Model C. Nested relations of those kind need to be persisted manually.
:param relation_field: field with relation leading to this model :type relation_field: Optional[ForeignKeyField] :param previous_model: previous model from which method came :type previous_model: Model :param exclude: items to exclude during saving of relations :type exclude: Union[Set, Dict] :param relation_map: map of relations to follow :type relation_map: Dict :param save_all: flag if all models should be saved or only not saved ones :type save_all: bool :param follow: flag to trigger deep save - by default only directly related models are saved with follow=True also related models of related models are saved :type follow: bool :param update_count: internal parameter for recursive calls - number of updated instances :type update_count: int :return: number of updated/saved models :rtype: int
Source code in ormar/models/model.py
113 114 115 116 117 118 119 120 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 203 204 205 206 207 208 209 210 211 212 213 214 |
|
update(_columns=None, **kwargs)
async
Performs update of Model instance in the database. Fields can be updated before or you can pass them as kwargs.
Sends pre_update and post_update signals.
Sets model save status to True.
:param _columns: list of columns to update, if None all are updated :type _columns: List :raises ModelPersistenceError: If the pk column is not set
:param kwargs: list of fields to update as field=value pairs :type kwargs: Any :return: updated Model :rtype: Model
Source code in ormar/models/model.py
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 |
|
upsert(**kwargs)
async
Performs either a save or an update depending on the presence of the pk. If the pk field is filled it's an update, otherwise the save is performed. For save kwargs are ignored, used only in update if provided.
:param kwargs: list of fields to update :type kwargs: Any :return: saved Model :rtype: Model
Source code in ormar/models/model.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
ModelDefinitionError
Bases: AsyncOrmException
Raised for errors related to the model definition itself:
- defining a Field without required parameters
- defining a model with more than one primary_key
- defining a model without primary_key
Source code in ormar/exceptions.py
14 15 16 17 18 19 20 21 22 23 |
|
MultipleMatches
Bases: AsyncOrmException
Raised for database queries that should return one row (i.e. get, first etc.) but has multiple matching results in response.
Source code in ormar/exceptions.py
42 43 44 45 46 47 48 |
|
NoMatch
Bases: AsyncOrmException
Raised for database queries that has no matching result (empty result).
Source code in ormar/exceptions.py
34 35 36 37 38 39 |
|
OrderAction
Bases: QueryAction
Order Actions is populated by queryset when order_by() is called.
All required params are extracted but kept raw until actual filter clause value is required -> then the action is converted into text() clause.
Extracted in order to easily change table prefixes on complex relations.
Source code in ormar/queryset/actions/order_action.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 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 88 89 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 |
|
check_if_filter_apply(target_model, alias)
Checks filter conditions to find if they apply to current join.
:param target_model: model which is now processed :type target_model: Type["Model"] :param alias: prefix of the relation :type alias: str :return: result of the check :rtype: bool
Source code in ormar/queryset/actions/order_action.py
103 104 105 106 107 108 109 110 111 112 113 114 |
|
get_field_name_text()
Escapes characters if it's required. Substitutes values of the models if value is a ormar Model with its pk value. Compiles the clause.
:return: complied and escaped clause :rtype: sqlalchemy.sql.elements.TextClause
Source code in ormar/queryset/actions/order_action.py
45 46 47 48 49 50 51 52 53 54 55 |
|
get_min_or_max()
Used in limit sub queries where you need to use aggregated functions in order to order by columns not included in group by. For postgres bool field it's using bool_or function as aggregates does not work with this type of columns.
:return: min or max function to order :rtype: sqlalchemy.sql.elements.TextClause
Source code in ormar/queryset/actions/order_action.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
get_text_clause()
Escapes characters if it's required. Substitutes values of the models if value is a ormar Model with its pk value. Compiles the clause.
:return: complied and escaped clause :rtype: sqlalchemy.sql.elements.TextClause
Source code in ormar/queryset/actions/order_action.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
QuerySet
Bases: Generic[T]
Main class to perform database queries, exposed on each model as objects attribute.
Source code in ormar/queryset/queryset.py
55 56 57 58 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 88 89 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 116 117 118 119 120 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 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 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 352 353 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 461 462 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 540 541 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 570 571 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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 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 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 |
|
database: databases.Database
property
Shortcut to models database from OrmarConfig class.
:return: database :rtype: databases.Database
model: Type[T]
property
Shortcut to model class set on QuerySet.
:return: model class :rtype: Type[Model]
model_config: OrmarConfig
property
Shortcut to model class OrmarConfig set on QuerySet model.
:return: OrmarConfig of the model :rtype: model's OrmarConfig
table: sqlalchemy.Table
property
Shortcut to models table from OrmarConfig.
:return: database table :rtype: sqlalchemy.Table
all(*args, **kwargs)
async
Returns all rows from a database for given model for set filter options.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).all()
.
If there are no rows meeting the criteria an empty list is returned.
:param kwargs: fields names and proper value types :type kwargs: Any :return: list of returned models :rtype: List[Model]
Source code in ormar/queryset/queryset.py
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 |
|
avg(columns)
async
Returns avg value of columns for rows matching the given criteria
(applied with filter
and exclude
if set before).
:return: avg value of columns :rtype: Union[int, float, List]
Source code in ormar/queryset/queryset.py
768 769 770 771 772 773 774 775 776 777 778 |
|
build_select_expression(limit=None, offset=None, order_bys=None)
Constructs the actual database query used in the QuerySet. If any of the params is not passed the QuerySet own value is used.
:param limit: number to limit the query :type limit: int :param offset: number to offset by :type offset: int :param order_bys: list of order-by fields names :type order_bys: List :return: built sqlalchemy select expression :rtype: sqlalchemy.sql.selectable.Select
Source code in ormar/queryset/queryset.py
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 |
|
bulk_create(objects)
async
Performs a bulk create in one database session to speed up the process.
Allows you to create multiple objects at once.
A valid list of Model
objects needs to be passed.
Bulk operations do not send signals.
:param objects: list of ormar models already initialized and ready to save. :type objects: List[Model]
Source code in ormar/queryset/queryset.py
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 |
|
bulk_update(objects, columns=None)
async
Performs bulk update in one database session to speed up the process.
Allows you to update multiple instance at once.
All Models
passed need to have primary key column populated.
You can also select which fields to update by passing columns
list
as a list of string names.
Bulk operations do not send signals.
:param objects: list of ormar models :type objects: List[Model] :param columns: list of columns to update :type columns: List[str]
Source code in ormar/queryset/queryset.py
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 |
|
check_single_result_rows_count(rows)
staticmethod
Verifies if the result has one and only one row.
:param rows: one element list of Models :type rows: List[Model]
Source code in ormar/queryset/queryset.py
234 235 236 237 238 239 240 241 242 243 244 245 |
|
count(distinct=True)
async
Returns number of rows matching the given criteria
(applied with filter
and exclude
if set before).
If distinct
is True
(the default), this will return
the number of primary rows selected. If False
,
the count will be the total number of rows returned
(including extra rows for one-to-many
or many-to-many
left select_related
table joins).
False
is the legacy (buggy) behavior for workflows that depend on it.
:param distinct: flag if the primary table rows should be distinct or not
:return: number of rows :rtype: int
Source code in ormar/queryset/queryset.py
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
|
create(**kwargs)
async
Creates the model instance, saves it in a database and returns the updates model (with pk populated if not passed and autoincrement is set).
The allowed kwargs are Model
fields names and proper value types.
:param kwargs: fields names and proper value types :type kwargs: Any :return: created model :rtype: Model
Source code in ormar/queryset/queryset.py
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 |
|
delete(*args, each=False, **kwargs)
async
Deletes from the model table after applying the filters from kwargs.
You have to either pass a filter to narrow down a query or explicitly pass each=True flag to affect whole table.
:param each: flag if whole table should be affected if no filter is passed :type each: bool :param kwargs: fields names and proper value types :type kwargs: Any :return: number of deleted rows :rtype:int
Source code in ormar/queryset/queryset.py
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 |
|
exclude(*args, **kwargs)
Works exactly the same as filter and all modifiers (suffixes) are the same, but returns a not condition.
So if you use filter(name='John')
which is where name = 'John'
in SQL,
the exclude(name='John')
equals to where name <> 'John'
Note that all conditions are joined so if you pass multiple values it becomes a union of conditions.
exclude(name='John', age>=35)
will become
where not (name='John' and age>=35)
:param kwargs: fields names and proper value types :type kwargs: Any :return: filtered QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
|
exclude_fields(columns)
With exclude_fields()
you can select subset of model columns that will
be excluded to limit the data load.
It's the opposite of fields()
method so check documentation above
to see what options are available.
Especially check above how you can pass also nested dictionaries and sets as a mask to exclude fields from whole hierarchy.
Note that fields()
and exclude_fields()
works both for main models
(on normal queries like get
, all
etc.)
as well as select_related
and prefetch_related
models
(with nested notation).
Mandatory fields cannot be excluded as it will raise ValidationError
,
to exclude a field it has to be nullable.
Pk column cannot be excluded - it's always auto added even if explicitly excluded.
:param columns: columns to exclude :type columns: Union[List, str, Set, Dict] :return: QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
|
exists()
async
Returns a bool value to confirm if there are rows matching the given criteria
(applied with filter
and exclude
if set).
:return: result of the check :rtype: bool
Source code in ormar/queryset/queryset.py
679 680 681 682 683 684 685 686 687 688 689 |
|
fields(columns, _is_exclude=False)
With fields()
you can select subset of model columns to limit the data load.
Note that fields()
and exclude_fields()
works both for main models
(on normal queries like get
, all
etc.)
as well as select_related
and prefetch_related
models (with nested notation).
You can select specified fields by passing a str, List[str], Set[str] or
dict
with nested definition.
To include related models use notation
{related_name}__{column}[__{optional_next} etc.]
.
fields()
can be called several times, building up the columns to select.
If you include related models into select_related()
call but you won't specify
columns for those models in fields - implies a list of all fields for
those nested models.
Mandatory fields cannot be excluded as it will raise ValidationError
,
to exclude a field it has to be nullable.
Pk column cannot be excluded - it's always auto added even if not explicitly included.
You can also pass fields to include as dictionary or set.
To mark a field as included in a dictionary use it's name as key and ellipsis as value.
To traverse nested models use nested dictionaries.
To include fields at last level instead of nested dictionary a set can be used.
To include whole nested model specify model related field name and ellipsis.
:param _is_exclude: flag if it's exclude or include operation :type _is_exclude: bool :param columns: columns to include :type columns: Union[List, str, Set, Dict] :return: QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
460 461 462 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 |
|
filter(*args, _exclude=False, **kwargs)
Allows you to filter by any Model
attribute/field
as well as to fetch instances, with a filter across an FK relationship.
You can use special filter suffix to change the filter operands:
- exact - like
album__name__exact='Malibu'
(exact match) - iexact - like
album__name__iexact='malibu'
(exact match case insensitive) - contains - like
album__name__contains='Mal'
(sql like) - icontains - like
album__name__icontains='mal'
(sql like case insensitive) - in - like
album__name__in=['Malibu', 'Barclay']
(sql in) - isnull - like
album__name__isnull=True
(sql is null) (isnotnullalbum__name__isnull=False
(sql is not null)) - gt - like
position__gt=3
(sql >) - gte - like
position__gte=3
(sql >=) - lt - like
position__lt=3
(sql <) - lte - like
position__lte=3
(sql <=) - startswith - like
album__name__startswith='Mal'
(exact start match) - istartswith - like
album__name__istartswith='mal'
(case insensitive) - endswith - like
album__name__endswith='ibu'
(exact end match) - iendswith - like
album__name__iendswith='IBU'
(case insensitive)
Note that you can also use python style filters - check the docs!
:param _exclude: flag if it should be exclude or filter :type _exclude: bool :param kwargs: fields names and proper value types :type kwargs: Any :return: filtered QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
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 352 353 354 |
|
first(*args, **kwargs)
async
Gets the first row from the db ordered by primary key column ascending.
:raises NoMatch: if no rows are returned :raises MultipleMatches: if more than 1 row is returned. :param kwargs: fields names and proper value types :type kwargs: Any :return: returned model :rtype: Model
Source code in ormar/queryset/queryset.py
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 |
|
first_or_none(*args, **kwargs)
async
Gets the first row from the db ordered by primary key column ascending.
If no match is found None will be returned.
:raises MultipleMatches: if more than 1 row is returned. :param kwargs: fields names and proper value types :type kwargs: Any :return: returned model :rtype: Model
Source code in ormar/queryset/queryset.py
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 |
|
get(*args, **kwargs)
async
Gets the first row from the db meeting the criteria set by kwargs.
If no criteria set it will return the last row in db sorted by pk.
Passing a criteria is actually calling filter(args, *kwargs) method described below.
:raises NoMatch: if no rows are returned :raises MultipleMatches: if more than 1 row is returned. :param kwargs: fields names and proper value types :type kwargs: Any :return: returned model :rtype: Model
Source code in ormar/queryset/queryset.py
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 |
|
get_or_create(_defaults=None, *args, **kwargs)
async
Combination of create and get methods.
Tries to get a row meeting the criteria for kwargs
and if NoMatch
exception is raised
it creates a new one with given kwargs and _defaults.
Passing a criteria is actually calling filter(args, *kwargs) method described below.
:param kwargs: fields names and proper value types :type kwargs: Any :param _defaults: default values for creating object :type _defaults: Optional[Dict[str, Any]] :return: model instance and a boolean :rtype: Tuple("T", bool)
Source code in ormar/queryset/queryset.py
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 |
|
get_or_none(*args, **kwargs)
async
Gets the first row from the db meeting the criteria set by kwargs.
If no criteria set it will return the last row in db sorted by pk.
Passing a criteria is actually calling filter(args, *kwargs) method described below.
If no match is found None will be returned.
:raises MultipleMatches: if more than 1 row is returned. :param kwargs: fields names and proper value types :type kwargs: Any :return: returned model :rtype: Model
Source code in ormar/queryset/queryset.py
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
|
iterate(*args, **kwargs)
async
Return async iterable generator for all rows from a database for given model.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).iterate()
.
If there are no rows meeting the criteria an empty async generator is returned.
:param kwargs: fields names and proper value types :type kwargs: Any :return: asynchronous iterable generator of returned models :rtype: AsyncGenerator[Model]
Source code in ormar/queryset/queryset.py
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 |
|
limit(limit_count, limit_raw_sql=None)
You can limit the results to desired number of parent models.
To limit the actual number of database query rows instead of number of main
models use the limit_raw_sql
parameter flag, and set it to True
.
:param limit_raw_sql: flag if raw sql should be limited :type limit_raw_sql: bool :param limit_count: number of models to limit :type limit_count: int :return: QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 |
|
max(columns)
async
Returns max value of columns for rows matching the given criteria
(applied with filter
and exclude
if set before).
:return: max value of column(s) :rtype: Any
Source code in ormar/queryset/queryset.py
732 733 734 735 736 737 738 739 740 741 742 |
|
min(columns)
async
Returns min value of columns for rows matching the given criteria
(applied with filter
and exclude
if set before).
:return: min value of column(s) :rtype: Any
Source code in ormar/queryset/queryset.py
744 745 746 747 748 749 750 751 752 753 754 |
|
offset(offset, limit_raw_sql=None)
You can also offset the results by desired number of main models.
To offset the actual number of database query rows instead of number of main
models use the limit_raw_sql
parameter flag, and set it to True
.
:param limit_raw_sql: flag if raw sql should be offset :type limit_raw_sql: bool :param offset: numbers of models to offset :type offset: int :return: QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 |
|
order_by(columns)
With order_by()
you can order the results from database based on your
choice of fields.
You can provide a string with field name or list of strings with fields names.
Ordering in sql will be applied in order of names you provide in order_by.
By default if you do not provide ordering ormar
explicitly orders by
all primary keys
If you are sorting by nested models that causes that the result rows are
unsorted by the main model ormar
will combine those children rows into
one main model.
The main model will never duplicate in the result
To order by main model field just provide a field name
To sort on nested models separate field names with dunder '__'.
You can sort this way across all relation types -> ForeignKey
,
reverse virtual FK and ManyToMany
fields.
To sort in descending order provide a hyphen in front of the field name
:param columns: columns by which models should be sorted :type columns: Union[List, str] :return: QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
|
paginate(page, page_size=20)
You can paginate the result which is a combination of offset and limit clauses. Limit is set to page size and offset is set to (page-1) * page_size.
:param page_size: numbers of items per page :type page_size: int :param page: page number :type page: int :return: QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
|
prefetch_related(related)
Allows to prefetch related models during query - but opposite to
select_related
each subsequent model is fetched in a separate database query.
With prefetch_related
always one query per Model is run against the
database, meaning that you will have multiple queries executed one
after another.
To fetch related model use ForeignKey
names.
To chain related Models
relation use double underscores between names.
:param related: list of relation field names, can be linked by '__' to nest :type related: Union[List, str] :return: QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
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 |
|
rebuild_self(filter_clauses=None, exclude_clauses=None, select_related=None, limit_count=None, offset=None, excludable=None, order_bys=None, prefetch_related=None, limit_raw_sql=None, proxy_source_model=None)
Method that returns new instance of queryset based on passed params, all not passed params are taken from current values.
Source code in ormar/queryset/queryset.py
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
select_all(follow=False)
By default adds only directly related models.
If follow=True is set it adds also related models of related models.
To not get stuck in an infinite loop as related models also keep a relation to parent model visited models set is kept.
That way already visited models that are nested are loaded, but the load do not follow them inside. So Model A -> Model B -> Model C -> Model A -> Model X will load second Model A but will never follow into Model X. Nested relations of those kind need to be loaded manually.
:param follow: flag to trigger deep save - by default only directly related models are saved with follow=True also related models of related models are saved :type follow: bool :return: reloaded Model :rtype: Model
Source code in ormar/queryset/queryset.py
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 |
|
select_related(related)
Allows to prefetch related models during the same query.
With select_related
always only one query is run against the database,
meaning that one (sometimes complicated) join is generated and later nested
models are processed in python.
To fetch related model use ForeignKey
names.
To chain related Models
relation use double underscores between names.
:param related: list of relation field names, can be linked by '__' to nest :type related: Union[List, str] :return: QuerySet :rtype: QuerySet
Source code in ormar/queryset/queryset.py
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 |
|
sum(columns)
async
Returns sum value of columns for rows matching the given criteria
(applied with filter
and exclude
if set before).
:return: sum value of columns :rtype: int
Source code in ormar/queryset/queryset.py
756 757 758 759 760 761 762 763 764 765 766 |
|
update(each=False, **kwargs)
async
Updates the model table after applying the filters from kwargs.
You have to either pass a filter to narrow down a query or explicitly pass each=True flag to affect whole table.
:param each: flag if whole table should be affected if no filter is passed :type each: bool :param kwargs: fields names and proper value types :type kwargs: Any :return: number of updated rows :rtype: int
Source code in ormar/queryset/queryset.py
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 |
|
update_or_create(**kwargs)
async
Updates the model, or in case there is no match in database creates a new one.
:param kwargs: fields names and proper value types :type kwargs: Any :return: updated or created model :rtype: Model
Source code in ormar/queryset/queryset.py
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 |
|
values(fields=None, exclude_through=False, _as_dict=True, _flatten=False)
async
Return a list of dictionaries with column values in order of the fields passed or all fields from queried models.
To filter for given row use filter/exclude methods before values, to limit number of rows use limit/offset or paginate before values.
Note that it always return a list even for one row from database.
:param exclude_through: flag if through models should be excluded :type exclude_through: bool :param _flatten: internal parameter to flatten one element tuples :type _flatten: bool :param _as_dict: internal parameter if return dict or tuples :type _as_dict: bool :param fields: field name or list of field names to extract from db :type fields: Union[List, str, Set, Dict]
Source code in ormar/queryset/queryset.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
|
values_list(fields=None, flatten=False, exclude_through=False)
async
Return a list of tuples with column values in order of the fields passed or all fields from queried models.
When one field is passed you can flatten the list of tuples into list of values of that single field.
To filter for given row use filter/exclude methods before values, to limit number of rows use limit/offset or paginate before values.
Note that it always return a list even for one row from database.
:param exclude_through: flag if through models should be excluded :type exclude_through: bool :param fields: field name or list of field names to extract from db :type fields: Union[str, List[str]] :param flatten: when one field is passed you can flatten the list of tuples :type flatten: bool
Source code in ormar/queryset/queryset.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
|
ReferentialAction
Bases: Enum
Because the database management system(DBMS) enforces referential constraints, it must ensure data integrity if rows in a referenced table are to be deleted (or updated).
If dependent rows in referencing tables still exist, those references have to be considered.
SQL specifies 5 different referential actions that shall take place in such occurrences.
Source code in ormar/fields/referential_actions.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
RelationType
Bases: Enum
Different types of relations supported by ormar:
- ForeignKey = PRIMARY
- reverse ForeignKey = REVERSE
- ManyToMany = MULTIPLE
Source code in ormar/relations/relation.py
25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Signal
Signal that notifies all receiver functions. In ormar used by models to send pre_save, post_save etc. signals.
Source code in ormar/signals/signal.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 88 89 90 91 92 93 94 95 96 |
|
connect(receiver)
Connects given receiver function to the signal.
:raises SignalDefinitionError: if receiver is not callable or not accept **kwargs :param receiver: receiver function :type receiver: Callable
Source code in ormar/signals/signal.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
disconnect(receiver)
Removes the receiver function from the signal.
:param receiver: receiver function :type receiver: Callable :return: flag if receiver was removed :rtype: bool
Source code in ormar/signals/signal.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
send(sender, **kwargs)
async
Notifies all receiver functions with given kwargs :param sender: model that sends the signal :type sender: Type["Model"] :param kwargs: arguments passed to receivers :type kwargs: Any
Source code in ormar/signals/signal.py
84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
SmallInteger
Bases: Integer
, int
SmallInteger field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
627 628 629 630 631 632 633 634 635 636 637 638 |
|
String
Bases: ModelFieldFactory
, str
String field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
175 176 177 178 179 180 181 182 183 184 185 186 |
|
validate(**kwargs)
classmethod
Used to validate if all required parameters on a given field type are set. :param kwargs: all params passed during construction :type kwargs: Any
Source code in ormar/fields/model_fields.py
188 189 190 191 192 193 194 195 196 197 198 199 |
|
Text
Bases: ModelFieldFactory
, str
Text field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
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 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
269 270 271 272 273 274 275 276 277 278 279 280 |
|
Time
Bases: ModelFieldFactory
, time
Time field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
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 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
432 433 434 435 436 437 438 439 440 441 442 443 |
|
UUID
Bases: ModelFieldFactory
, UUID
UUID field factory that construct Field classes and populated their values.
Source code in ormar/fields/model_fields.py
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 |
|
get_column_type(**kwargs)
classmethod
Return proper type of db column for given field type. Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options :type kwargs: Any :return: initialized column with proper options :rtype: sqlalchemy Column
Source code in ormar/fields/model_fields.py
736 737 738 739 740 741 742 743 744 745 746 747 748 |
|
UniqueColumns
Bases: UniqueConstraint
Subclass of sqlalchemy.UniqueConstraint. Used to avoid importing anything from sqlalchemy by user.
Source code in ormar/fields/constraints.py
6 7 8 9 10 |
|
ForeignKey(to, *, name=None, unique=False, nullable=True, related_name=None, virtual=False, onupdate=None, ondelete=None, **kwargs)
Despite a name it's a function that returns constructed ForeignKeyField. This function is actually used in model declaration (as ormar.ForeignKey(ToModel)).
Accepts number of relation setting parameters as well as all BaseField ones.
:param to: target related ormar Model :type to: Model class :param name: name of the database field - later called alias :type name: str :param unique: parameter passed to sqlalchemy.ForeignKey, unique flag :type unique: bool :param nullable: marks field as optional/ required :type nullable: bool :param related_name: name of reversed FK relation populated for you on to model :type related_name: str :param virtual: marks if relation is virtual. It is for reversed FK and auto generated FK on through model in Many2Many relations. :type virtual: bool :param onupdate: parameter passed to sqlalchemy.ForeignKey. How to treat child rows on update of parent (the one where FK is defined) model. :type onupdate: Union[ReferentialAction, str] :param ondelete: parameter passed to sqlalchemy.ForeignKey. How to treat child rows on delete of parent (the one where FK is defined) model. :type ondelete: Union[ReferentialAction, str] :param kwargs: all other args to be populated by BaseField :type kwargs: Any :return: ormar ForeignKeyField with relation to selected model :rtype: ForeignKeyField
Source code in ormar/fields/foreign_key.py
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 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 |
|
ManyToMany(to, through=None, *, name=None, unique=False, virtual=False, **kwargs)
Despite a name it's a function that returns constructed ManyToManyField. This function is actually used in model declaration (as ormar.ManyToMany(ToModel, through=ThroughModel)).
Accepts number of relation setting parameters as well as all BaseField ones.
:param to: target related ormar Model :type to: Model class :param through: through model for m2m relation :type through: Model class :param name: name of the database field - later called alias :type name: str :param unique: parameter passed to sqlalchemy.ForeignKey, unique flag :type unique: bool :param virtual: marks if relation is virtual. It is for reversed FK and auto generated FK on through model in Many2Many relations. :type virtual: bool :param kwargs: all other args to be populated by BaseField :type kwargs: Any :return: ormar ManyToManyField with m2m relation to selected model :rtype: ManyToManyField
Source code in ormar/fields/many_to_many.py
87 88 89 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 116 117 118 119 120 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
and_(*args, **kwargs)
Construct and filter from nested groups and keyword arguments
:param args: nested filter groups :type args: Tuple[FilterGroup] :param kwargs: fields names and proper value types :type kwargs: Any :return: FilterGroup ready to be resolved :rtype: ormar.queryset.clause.FilterGroup
Source code in ormar/queryset/clause.py
147 148 149 150 151 152 153 154 155 156 157 158 |
|
or_(*args, **kwargs)
Construct or filter from nested groups and keyword arguments
:param args: nested filter groups :type args: Tuple[FilterGroup] :param kwargs: fields names and proper value types :type kwargs: Any :return: FilterGroup ready to be resolved :rtype: ormar.queryset.clause.FilterGroup
Source code in ormar/queryset/clause.py
133 134 135 136 137 138 139 140 141 142 143 144 |
|
post_bulk_update(senders)
Connect given function to all senders for post_bulk_update signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
176 177 178 179 180 181 182 183 184 185 186 |
|
post_delete(senders)
Connect given function to all senders for post_delete signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
70 71 72 73 74 75 76 77 78 79 80 |
|
post_relation_add(senders)
Connect given function to all senders for post_relation_add signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
135 136 137 138 139 140 141 142 143 144 145 |
|
post_relation_remove(senders)
Connect given function to all senders for post_relation_remove signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
post_save(senders)
Connect given function to all senders for post_save signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
44 45 46 47 48 49 50 51 52 53 54 |
|
post_update(senders)
Connect given function to all senders for post_update signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
57 58 59 60 61 62 63 64 65 66 67 |
|
pre_delete(senders)
Connect given function to all senders for pre_delete signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
109 110 111 112 113 114 115 116 117 118 119 |
|
pre_relation_add(senders)
Connect given function to all senders for pre_relation_add signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
122 123 124 125 126 127 128 129 130 131 132 |
|
pre_relation_remove(senders)
Connect given function to all senders for pre_relation_remove signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
148 149 150 151 152 153 154 155 156 157 158 |
|
pre_save(senders)
Connect given function to all senders for pre_save signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
83 84 85 86 87 88 89 90 91 92 93 |
|
pre_update(senders)
Connect given function to all senders for pre_update signal.
:param senders: one or a list of "Model" classes that should have the signal receiver registered :type senders: Union[Type["Model"], List[Type["Model"]]] :return: returns the original function untouched :rtype: Callable
Source code in ormar/decorators/signals.py
96 97 98 99 100 101 102 103 104 105 106 |
|