foreign_key
ForeignKeyConstraint
dataclass
Internal container to store ForeignKey definitions used later to produce sqlalchemy.ForeignKeys
Source code in ormar/fields/foreign_key.py
184 185 186 187 188 189 190 191 192 193 194 |
|
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 |
|
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 |
|
create_dummy_instance(fk, pk=None)
Ormar never returns you a raw data. So if you have a related field that has a value populated it will construct you a Model instance out of it.
Creates a "fake" instance of passed Model from pk value. The instantiated Model has only pk value filled. To achieve this pk_only flag has to be passed as it skips the validation.
If the nested related Models are required they are set with -1 as pk value.
:param fk: class of the related Model to which instance should be constructed :type fk: Model class :param pk: value of the primary_key column :type pk: Any :return: Model instance populated with only pk :rtype: Model
Source code in ormar/fields/foreign_key.py
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 |
|
create_dummy_model(base_model, pk_field)
Used to construct a dummy pydantic model for type hints and pydantic validation. Populates only pk field and set it to desired type.
:param base_model: class of target dummy model :type base_model: Model class :param pk_field: ormar Field to be set on pydantic Model :type pk_field: Union[BaseField, "ForeignKeyField", "ManyToManyField"] :return: constructed dummy model :rtype: pydantic.BaseModel
Source code in ormar/fields/foreign_key.py
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 |
|
populate_fk_params_based_on_to_model(to, nullable, onupdate=None, ondelete=None)
Based on target to model to which relation leads to populates the type of the pydantic field to use, ForeignKey constraint and type of the target column field.
:param to: target related ormar Model :type to: Model class :param nullable: marks field as optional/ required :type nullable: 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: 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: str :return: tuple with target pydantic type, list of fk constraints and target col type :rtype: Tuple[Any, List, Any]
Source code in ormar/fields/foreign_key.py
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 |
|
validate_not_allowed_fields(kwargs)
Verifies if not allowed parameters are set on relation models. Usually they are omitted later anyway but this way it's explicitly notify the user that it's not allowed/ supported.
:raises ModelDefinitionError: if any forbidden field is set :param kwargs: dict of kwargs to verify passed to relation field :type kwargs: Dict
Source code in ormar/fields/foreign_key.py
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 |
|
validate_referential_action(action)
Validation onupdate
and ondelete
action cast to a string value
:raises ModelDefinitionError: if action is a not valid name string value :param action: referential action attribute or name string :type action: Optional[Union[ReferentialAction, str]] :rtype: Optional[str]
Source code in ormar/fields/foreign_key.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|