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

Method defer

lib/sqlalchemy/orm/strategy_options.py:581–640  ·  view source on GitHub ↗

r"""Indicate that the given column-oriented attribute should be deferred, e.g. not loaded until accessed. This function is part of the :class:`_orm.Load` interface and supports both method-chained and standalone operation. e.g.:: from sqlalchemy.orm imp

(self, key: _AttrType, raiseload: bool = False)

Source from the content-addressed store, hash-verified

579 return self._set_relationship_strategy(attr, None)
580
581 def defer(self, key: _AttrType, raiseload: bool = False) -> Self:
582 r"""Indicate that the given column-oriented attribute should be
583 deferred, e.g. not loaded until accessed.
584
585 This function is part of the :class:`_orm.Load` interface and supports
586 both method-chained and standalone operation.
587
588 e.g.::
589
590 from sqlalchemy.orm import defer
591
592 session.query(MyClass).options(
593 defer(MyClass.attribute_one), defer(MyClass.attribute_two)
594 )
595
596 To specify a deferred load of an attribute on a related class,
597 the path can be specified one token at a time, specifying the loading
598 style for each link along the chain. To leave the loading style
599 for a link unchanged, use :func:`_orm.defaultload`::
600
601 session.query(MyClass).options(
602 defaultload(MyClass.someattr).defer(RelatedClass.some_column)
603 )
604
605 Multiple deferral options related to a relationship can be bundled
606 at once using :meth:`_orm.Load.options`::
607
608
609 select(MyClass).options(
610 defaultload(MyClass.someattr).options(
611 defer(RelatedClass.some_column),
612 defer(RelatedClass.some_other_column),
613 defer(RelatedClass.another_column),
614 )
615 )
616
617 :param key: Attribute to be deferred.
618
619 :param raiseload: raise :class:`.InvalidRequestError` rather than
620 lazy loading a value when the deferred attribute is accessed. Used
621 to prevent unwanted SQL from being emitted.
622
623 .. versionadded:: 1.4
624
625 .. seealso::
626
627 :ref:`orm_queryguide_column_deferral` - in the
628 :ref:`queryguide_toplevel`
629
630 :func:`_orm.load_only`
631
632 :func:`_orm.undefer`
633
634 """
635 strategy = {"deferred": True, "instrument": True}
636 if raiseload:
637 strategy["raiseload"] = True
638 return self._set_column_strategy(

Callers 15

load_options_okFunction · 0.80
load_options_errorFunction · 0.80
test_unbound_optionsMethod · 0.80
test_bound_optionsMethod · 0.80
test_set_strat_colMethod · 0.80
test_oneMethod · 0.80
test_twoMethod · 0.80

Calls 2

_set_column_strategyMethod · 0.95

Tested by 15

test_unbound_optionsMethod · 0.64
test_bound_optionsMethod · 0.64
test_set_strat_colMethod · 0.64
test_oneMethod · 0.64
test_twoMethod · 0.64
test_threeMethod · 0.64
test_fourMethod · 0.64