MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / association_proxy

Function association_proxy

lib/sqlalchemy/ext/associationproxy.py:85–265  ·  view source on GitHub ↗

r"""Return a Python property implementing a view of a target attribute which references an attribute on members of the target. The returned value is an instance of :class:`.AssociationProxy`. Implements a Python property representing a relationship as a collection of simpler va

(
    target_collection: str,
    attr: str,
    *,
    creator: Optional[_CreatorProtocol] = None,
    getset_factory: Optional[_GetSetFactoryProtocol] = None,
    proxy_factory: Optional[_ProxyFactoryProtocol] = None,
    proxy_bulk_set: Optional[_ProxyBulkSetProtocol] = None,
    info: Optional[_InfoType] = None,
    cascade_scalar_deletes: bool = False,
    create_on_none_assignment: bool = False,
    init: Union[_NoArg, bool] = _NoArg.NO_ARG,
    repr: Union[_NoArg, bool] = _NoArg.NO_ARG,  # noqa: A002
    default: Optional[Any] = _NoArg.NO_ARG,
    default_factory: Union[_NoArg, Callable[[], _T]] = _NoArg.NO_ARG,
    compare: Union[_NoArg, bool] = _NoArg.NO_ARG,
    kw_only: Union[_NoArg, bool] = _NoArg.NO_ARG,
    hash: Union[_NoArg, bool, None] = _NoArg.NO_ARG,  # noqa: A002
    dataclass_metadata: Union[_NoArg, Mapping[Any, Any], None] = _NoArg.NO_ARG,
)

Source from the content-addressed store, hash-verified

83
84
85def association_proxy(
86 target_collection: str,
87 attr: str,
88 *,
89 creator: Optional[_CreatorProtocol] = None,
90 getset_factory: Optional[_GetSetFactoryProtocol] = None,
91 proxy_factory: Optional[_ProxyFactoryProtocol] = None,
92 proxy_bulk_set: Optional[_ProxyBulkSetProtocol] = None,
93 info: Optional[_InfoType] = None,
94 cascade_scalar_deletes: bool = False,
95 create_on_none_assignment: bool = False,
96 init: Union[_NoArg, bool] = _NoArg.NO_ARG,
97 repr: Union[_NoArg, bool] = _NoArg.NO_ARG, # noqa: A002
98 default: Optional[Any] = _NoArg.NO_ARG,
99 default_factory: Union[_NoArg, Callable[[], _T]] = _NoArg.NO_ARG,
100 compare: Union[_NoArg, bool] = _NoArg.NO_ARG,
101 kw_only: Union[_NoArg, bool] = _NoArg.NO_ARG,
102 hash: Union[_NoArg, bool, None] = _NoArg.NO_ARG, # noqa: A002
103 dataclass_metadata: Union[_NoArg, Mapping[Any, Any], None] = _NoArg.NO_ARG,
104) -> AssociationProxy[Any]:
105 r"""Return a Python property implementing a view of a target
106 attribute which references an attribute on members of the
107 target.
108
109 The returned value is an instance of :class:`.AssociationProxy`.
110
111 Implements a Python property representing a relationship as a collection
112 of simpler values, or a scalar value. The proxied property will mimic
113 the collection type of the target (list, dict or set), or, in the case of
114 a one to one relationship, a simple scalar value.
115
116 :param target_collection: Name of the attribute that is the immediate
117 target. This attribute is typically mapped by
118 :func:`~sqlalchemy.orm.relationship` to link to a target collection, but
119 can also be a many-to-one or non-scalar relationship.
120
121 :param attr: Attribute on the associated instance or instances that
122 are available on instances of the target object.
123
124 :param creator: optional.
125
126 Defines custom behavior when new items are added to the proxied
127 collection.
128
129 By default, adding new items to the collection will trigger a
130 construction of an instance of the target object, passing the given
131 item as a positional argument to the target constructor. For cases
132 where this isn't sufficient, :paramref:`.association_proxy.creator`
133 can supply a callable that will construct the object in the
134 appropriate way, given the item that was passed.
135
136 For list- and set- oriented collections, a single argument is
137 passed to the callable. For dictionary oriented collections, two
138 arguments are passed, corresponding to the key and value.
139
140 The :paramref:`.association_proxy.creator` callable is also invoked
141 for scalar (i.e. many-to-one, one-to-one) relationships. If the
142 current value of the target relationship attribute is ``None``, the

Callers 15

UserClass · 0.90
UserClass · 0.90
user_idsMethod · 0.90
ParentClass · 0.90
ParentClass · 0.90
ParentClass · 0.90
ParentClass · 0.90
AClass · 0.90
ParentClass · 0.90
setup_classesMethod · 0.90
UserClass · 0.90

Calls 2

AssociationProxyClass · 0.85
_AttributeOptionsClass · 0.85

Tested by 10

setup_classesMethod · 0.72
childrenMethod · 0.72
test_constructorMethod · 0.72
test_emptyMethod · 0.72
addressesMethod · 0.72
user_idsMethod · 0.72
filtersMethod · 0.72
user_idsMethod · 0.72