add safe fails for adding and removing not saved models to many to many rel, add tests for save_related

This commit is contained in:
collerek
2020-11-14 14:47:33 +01:00
parent cd33f6a96b
commit 0f36944fe1
4 changed files with 41 additions and 6 deletions

View File

@ -1,7 +1,7 @@
from typing import Any, TYPE_CHECKING
import ormar
from ormar.exceptions import RelationshipInstanceError
from ormar.exceptions import NoMatch, RelationshipInstanceError
from ormar.relations.querysetproxy import QuerysetProxy
if TYPE_CHECKING: # pragma no cover
@ -54,6 +54,11 @@ class RelationProxy(list):
return queryset
async def remove(self, item: "Model") -> None: # type: ignore
if item not in self:
raise NoMatch(
f"Object {self._owner.get_name()} has no "
f"{item.get_name()} with given primary key!"
)
super().remove(item)
rel_name = item.resolve_relation_name(item, self._owner)
relation = item._orm._get(rel_name)