MCPcopy
hub / github.com/ormar-orm/ormar / remove

Method remove

ormar/relations/relation_proxy.py:288–342  ·  view source on GitHub ↗

Removes the related from relation with parent. Through models are automatically deleted for m2m relations. For reverse FK relations keep_reversed flag marks if the reversed models should be kept or deleted from the database too (False means that models will

(  # type: ignore
        self, item: "T", keep_reversed: bool = True
    )

Source from the content-addressed store, hash-verified

286 return queryset
287
288 async def remove( # type: ignore
289 self, item: "T", keep_reversed: bool = True
290 ) -> None:
291 """
292 Removes the related from relation with parent.
293
294 Through models are automatically deleted for m2m relations.
295
296 For reverse FK relations keep_reversed flag marks if the reversed models
297 should be kept or deleted from the database too (False means that models
298 will be deleted, and not only removed from relation).
299
300 :param item: child to remove from relation
301 :type item: Model
302 :param keep_reversed: flag if the reversed model should be kept or deleted too
303 :type keep_reversed: bool
304 """
305 if item not in self:
306 raise NoMatch(
307 f"Object {self._owner.get_name()} has no "
308 f"{item.get_name()} with given primary key!"
309 )
310 await self._owner.signals.pre_relation_remove.send(
311 sender=self._owner.__class__,
312 instance=self._owner,
313 child=item,
314 relation_name=self.field_name,
315 )
316
317 index_to_remove = self._relation_cache[item.__hash__()]
318 self.pop(index_to_remove)
319
320 relation_name = self.related_field_name
321 relation = item._orm._get(relation_name)
322 # if relation is None: # pragma nocover
323 # raise ValueError(
324 # f"{self._owner.get_name()} does not have relation {relation_name}"
325 # )
326 if relation:
327 relation.remove(self._owner)
328 self.relation.remove(item)
329 if self.type_ == ormar.RelationType.MULTIPLE:
330 await self.queryset_proxy.delete_through_instance(item)
331 else:
332 if keep_reversed:
333 setattr(item, relation_name, None)
334 await item.update()
335 else:
336 await item.delete()
337 await self._owner.signals.post_relation_remove.send(
338 sender=self._owner.__class__,
339 instance=self._owner,
340 child=item,
341 relation_name=self.field_name,
342 )
343
344 async def add(self, item: "T", **kwargs: Any) -> None:
345 """

Callers

nothing calls this directly

Calls 9

popMethod · 0.95
NoMatchClass · 0.90
get_nameMethod · 0.80
sendMethod · 0.80
_getMethod · 0.80
__hash__Method · 0.45
updateMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected