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

Method with_parent

lib/sqlalchemy/orm/query.py:1251–1318  ·  view source on GitHub ↗

Add filtering criterion that relates the given instance to a child object or collection, using its attribute state as well as an established :func:`_orm.relationship()` configuration. The method uses the :func:`.with_parent` function to generate the clause, t

(
        self,
        instance: object,
        property: Optional[  # noqa: A002
            attributes.QueryableAttribute[Any]
        ] = None,
        from_entity: Optional[_ExternalEntityType[Any]] = None,
    )

Source from the content-addressed store, hash-verified

1249 )
1250 @util.preload_module("sqlalchemy.orm.relationships")
1251 def with_parent(
1252 self,
1253 instance: object,
1254 property: Optional[ # noqa: A002
1255 attributes.QueryableAttribute[Any]
1256 ] = None,
1257 from_entity: Optional[_ExternalEntityType[Any]] = None,
1258 ) -> Self:
1259 """Add filtering criterion that relates the given instance
1260 to a child object or collection, using its attribute state
1261 as well as an established :func:`_orm.relationship()`
1262 configuration.
1263
1264 The method uses the :func:`.with_parent` function to generate
1265 the clause, the result of which is passed to
1266 :meth:`_query.Query.filter`.
1267
1268 Parameters are the same as :func:`.with_parent`, with the exception
1269 that the given property can be None, in which case a search is
1270 performed against this :class:`_query.Query` object's target mapper.
1271
1272 :param instance:
1273 An instance which has some :func:`_orm.relationship`.
1274
1275 :param property:
1276 Class bound attribute which indicates
1277 what relationship from the instance should be used to reconcile the
1278 parent/child relationship.
1279
1280 :param from_entity:
1281 Entity in which to consider as the left side. This defaults to the
1282 "zero" entity of the :class:`_query.Query` itself.
1283
1284 """
1285 relationships = util.preloaded.orm_relationships
1286
1287 if from_entity:
1288 entity_zero = inspect(from_entity)
1289 else:
1290 entity_zero = _legacy_filter_by_entity_zero(self)
1291 if property is None:
1292 # TODO: deprecate, property has to be supplied
1293 mapper = object_mapper(instance)
1294
1295 for prop in mapper.iterate_properties:
1296 if (
1297 isinstance(prop, relationships.RelationshipProperty)
1298 and prop.mapper is entity_zero.mapper # type: ignore
1299 ):
1300 property = prop # type: ignore # noqa: A001
1301 break
1302 else:
1303 raise sa_exc.InvalidRequestError(
1304 "Could not locate a property which relates instances "
1305 "of class '%s' to instances of class '%s'"
1306 % (
1307 entity_zero.mapper.class_.__name__, # type: ignore
1308 instance.__class__.__name__,

Calls 5

filterMethod · 0.95
inspectFunction · 0.90
object_mapperFunction · 0.85
with_parentFunction · 0.85