Skip to content

Commit

Permalink
Hotfix with get_or_none in Model classmethod (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
hqsz authored Feb 25, 2020
1 parent 93995aa commit 8fa5e9e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Changelog
=========

0.15.17
-------
- Now ``get_or_none(...)``, classmethod of ``Model`` class, works in the same way as ``queryset``

0.15.16
-------
- ``get_or_none(...)`` now raises ``MultipleObjectsReturned`` if multiple object fetched. (#298)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_model_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ async def test_get_or_none(self):
mdl = await self.cls.get_or_none(name="Test2")
self.assertEqual(mdl, None)

await self.cls.create(name="Test")

with self.assertRaises(MultipleObjectsReturned):
await self.cls.get_or_none(name="Test")


class TestModelMethodsNoID(TestModelMethods):
async def setUp(self):
Expand Down
2 changes: 1 addition & 1 deletion tortoise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ def get_or_none(cls: Type[MODEL], *args: Q, **kwargs: Any) -> QuerySetSingle[Opt
:param args:
:param kwargs:
"""
return QuerySet(cls).filter(*args, **kwargs).first()
return QuerySet(cls).get_or_none(*args, **kwargs)

@classmethod
async def fetch_for_list(
Expand Down

0 comments on commit 8fa5e9e

Please sign in to comment.