Adds child model to relation. For ManyToMany relations through instance is automatically created. :param kwargs: dict of additional keyword arguments for through instance :type kwargs: Any :param item: child to add to relation :type item: Model
(self, item: "T", **kwargs: Any)
| 342 | ) |
| 343 | |
| 344 | async def add(self, item: "T", **kwargs: Any) -> None: |
| 345 | """ |
| 346 | Adds child model to relation. |
| 347 | |
| 348 | For ManyToMany relations through instance is automatically created. |
| 349 | |
| 350 | :param kwargs: dict of additional keyword arguments for through instance |
| 351 | :type kwargs: Any |
| 352 | :param item: child to add to relation |
| 353 | :type item: Model |
| 354 | """ |
| 355 | new_idx = len(self) if item not in self else self.index(item) |
| 356 | relation_name = self.related_field_name |
| 357 | await self._owner.signals.pre_relation_add.send( |
| 358 | sender=self._owner.__class__, |
| 359 | instance=self._owner, |
| 360 | child=item, |
| 361 | relation_name=self.field_name, |
| 362 | passed_kwargs=kwargs, |
| 363 | ) |
| 364 | self._check_if_model_saved() |
| 365 | if self.type_ == ormar.RelationType.MULTIPLE: |
| 366 | await self.queryset_proxy.create_through_instance(item, **kwargs) |
| 367 | setattr(self._owner, self.field_name, item) |
| 368 | else: |
| 369 | setattr(item, relation_name, self._owner) |
| 370 | await item.upsert() |
| 371 | self._relation_cache[item.__hash__()] = new_idx |
| 372 | await self._owner.signals.post_relation_add.send( |
| 373 | sender=self._owner.__class__, |
| 374 | instance=self._owner, |
| 375 | child=item, |
| 376 | relation_name=self.field_name, |
| 377 | passed_kwargs=kwargs, |
| 378 | ) |
no test coverage detected