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

Method load_only

lib/sqlalchemy/orm/strategy_options.py:163–236  ·  view source on GitHub ↗

r"""Indicate that for a particular entity, only the given list of column-based attribute names should be loaded; all others will be deferred. This function is part of the :class:`_orm.Load` interface and supports both method-chained and standalone operation.

(self, *attrs: _AttrType, raiseload: bool = False)

Source from the content-addressed store, hash-verified

161 return cloned
162
163 def load_only(self, *attrs: _AttrType, raiseload: bool = False) -> Self:
164 r"""Indicate that for a particular entity, only the given list
165 of column-based attribute names should be loaded; all others will be
166 deferred.
167
168 This function is part of the :class:`_orm.Load` interface and supports
169 both method-chained and standalone operation.
170
171 Example - given a class ``User``, load only the ``name`` and
172 ``fullname`` attributes::
173
174 session.query(User).options(load_only(User.name, User.fullname))
175
176 Example - given a relationship ``User.addresses -> Address``, specify
177 subquery loading for the ``User.addresses`` collection, but on each
178 ``Address`` object load only the ``email_address`` attribute::
179
180 session.query(User).options(
181 subqueryload(User.addresses).load_only(Address.email_address)
182 )
183
184 For a statement that has multiple entities,
185 the lead entity can be
186 specifically referred to using the :class:`_orm.Load` constructor::
187
188 stmt = (
189 select(User, Address)
190 .join(User.addresses)
191 .options(
192 Load(User).load_only(User.name, User.fullname),
193 Load(Address).load_only(Address.email_address),
194 )
195 )
196
197 When used together with the
198 :ref:`populate_existing <orm_queryguide_populate_existing>`
199 execution option only the attributes listed will be refreshed.
200
201 :param \*attrs: Attributes to be loaded, all others will be deferred.
202
203 :param raiseload: raise :class:`.InvalidRequestError` rather than
204 lazy loading a value when a deferred attribute is accessed. Used
205 to prevent unwanted SQL from being emitted.
206
207 .. versionadded:: 2.0
208
209 .. seealso::
210
211 :ref:`orm_queryguide_column_deferral` - in the
212 :ref:`queryguide_toplevel`
213
214 :param \*attrs: Attributes to be loaded, all others will be deferred.
215
216 :param raiseload: raise :class:`.InvalidRequestError` rather than
217 lazy loading a value when a deferred attribute is accessed. Used
218 to prevent unwanted SQL from being emitted.
219
220 .. versionadded:: 2.0

Calls 2

_set_column_strategyMethod · 0.95