Association fields
Fields for establishing relationships between domain elements -- connecting entities and aggregates.
See Association Fields reference for usage examples.
HasOne
HasOne(to_cls: str | type, **kwargs: Any)
Bases: Association
Represents an one-to-one association between an aggregate and its entities.
This field is used to define a relationship where an aggregate is associated with at most one instance of a child entity.
Source code in src/protean/fields/association.py
280 281 282 283 284 285 286 287 288 289 | |
__set__
__set__(instance: Any, value: Any) -> None
Setup relationship to be persisted/updated
We track the change in the instance's _temp_cache to determine if the relationship
has been added, updated, or deleted. We track two aspects: state and old value.
For HasOne, there are three possible states:
- ADDED: The relationship is being added for the first time
- UPDATED: The relationship is being updated
- DELETED: The relationship is being removed
Of these, the old value is applicable for UPDATED and DELETED states.
Also, we recursively remove child entities if they are associated with the old value.
The temp_cache we set up here is eventually used by the Repository to determine
the changes to be persisted.
Source code in src/protean/fields/association.py
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 | |
as_dict
as_dict(value: Any) -> Any
Return JSON-compatible value of self
Source code in src/protean/fields/association.py
548 549 550 551 | |
HasMany
HasMany(to_cls: str | type, **kwargs: Any)
Bases: Association
Represents a one-to-many association between two entities. This field is used to define a relationship where an aggregate has multiple instances of a chil entity.
| PARAMETER | DESCRIPTION |
|---|---|
to_cls
|
The class of the target entity.
TYPE:
|
**kwargs
|
Additional keyword arguments to be passed to the base field class.
TYPE:
|
Source code in src/protean/fields/association.py
280 281 282 283 284 285 286 287 288 289 | |
__set__
__set__(instance: Any, value: Any) -> None
This supports direct assignment of values to HasMany fields, like:
order.items = [item1, item2, item3]
Source code in src/protean/fields/association.py
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 | |
add
add(instance: Any, items: Any) -> None
Available as add_<HasMany Field Name> method on the entity instance.
This method adds one or more linked entities to the source entity. It also diffs the current value with the new value to determine the changes that need to be persisted.
The method also takes care of the attributes of the linked entities, preparing them for persistence.
Each linkage takes care of its own attributes, preparing them for persistence. One exception is deletion of an entity - all child entities have to be marked as removed.
We track the change in the instance's _temp_cache to determine if the relationship
has been added, updated, or deleted. We track each item's state and group the changes
into three buckets:
- ADDED: The relationship is being added for the first time
- UPDATED: The relationship is being updated
- DELETED: The relationship is being removed
The DELETED objects are detected from the pool of new objects, but it is also possible to remove them
directly with the remove method.
The temp_cache we set up here is eventually used by the Repository to determine
the changes to be persisted.
| PARAMETER | DESCRIPTION |
|---|---|
instance
|
The source entity instance.
TYPE:
|
items
|
The linked entity or entities to be added.
TYPE:
|
Source code in src/protean/fields/association.py
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 | |
remove
remove(instance: Any, items: Any) -> None
Available as remove_<HasMany Field Name> method on the entity instance.
Remove one or more linked entities from the source entity.
We also recursively remove child entities if they are associated with the removed value.
| PARAMETER | DESCRIPTION |
|---|---|
instance
|
The source entity instance.
TYPE:
|
items
|
The linked entity or entities to be removed.
TYPE:
|
Source code in src/protean/fields/association.py
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 | |
as_dict
as_dict(value: Any) -> list[Any]
Return JSON-compatible value of self.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value to be converted to a JSON-compatible format.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list
|
A list of dictionaries representing the linked entities.
TYPE:
|
Source code in src/protean/fields/association.py
838 839 840 841 842 843 844 845 846 847 848 | |
get
get(instance: Any, **kwargs: Any) -> Any
Fetch a single linked entity based on the provided criteria.
Available as get_one_from_<HasMany Field Name> method on the entity instance.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
The filtering criteria.
TYPE:
|
Source code in src/protean/fields/association.py
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 | |
filter
filter(instance: Any, **kwargs: Any) -> list[Any]
Filter the linked entities based on the provided criteria.
Available as filter_<HasMany Field Name> method on the entity instance.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
The filtering criteria.
TYPE:
|
Source code in src/protean/fields/association.py
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | |
Reference
Reference(to_cls: str | type, **kwargs: Any)
Bases: FieldCacheMixin, Field
A field representing a reference to another entity. This field is used to establish the reverse relationship to the remote entity.
| PARAMETER | DESCRIPTION |
|---|---|
to_cls
|
The target entity class or its name.
TYPE:
|
**kwargs
|
Additional keyword arguments to be passed to the base
TYPE:
|
Source code in src/protean/fields/association.py
107 108 109 110 111 | |
linked_attribute
property
linked_attribute: str
Return linkage attribute to the target class
This method is initially called from __set_name__() -> get_attribute_name()
at which point, the to_cls has not been initialized properly. We simply default
the linked attribute to 'id' in that case.
Eventually, when setting value the first time, the to_cls entity is initialized
and the attribute name is reset correctly.
get_attribute_name
get_attribute_name() -> str
Return formatted attribute name for the shadow field
Source code in src/protean/fields/association.py
117 118 119 120 121 | |
get_shadow_field
get_shadow_field() -> tuple[str | None, _ReferenceField]
Return shadow field Primarily used during Entity initialization to register shadow field
Source code in src/protean/fields/association.py
123 124 125 126 | |
__get__
__get__(instance: Any, owner: Any) -> Any
Retrieve associated objects
Source code in src/protean/fields/association.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
__set__
__set__(instance: Any, value: Any) -> None
Override __set__ to coordinate between relation field and its shadow attribute
Source code in src/protean/fields/association.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
as_dict
as_dict(value: Any) -> Any
Return JSON-compatible value of self
Source code in src/protean/fields/association.py
259 260 261 | |