tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.37k stars 355 forks source link

[#1635]Add model name in DoesNotExist and MultipleObjectsReturned message #1637

Closed BraveEggTart closed 3 weeks ago

BraveEggTart commented 3 weeks ago

Description

Add the model.name field to the DoesNotExist and MultipleObjectsReturned exception message to obtain clearer prompts when calling the model. get() method.

Motivation and Context

When I use the Model.get() method and a DoesNotExist exception occurs because there is no corresponding record in the database, the provided error message is often not specific enough for me to quickly identify which Model is involved. The message "Object does not exist" is too generic.

#1635

How Has This Been Tested?

I executed the make test command and passed all tests related to the get() method, such as the test_get method in test_queryset.py.

testing environment: python: 3.10.6

There's something I need to say. I checked the code about DoesNotExist and MultipleObjectsReturned, and noticed that in the _getbypk method of models.py, DoesNotExist was processed and returned cls._meta.full_name, Is this a place that needs to be modified synchronously?

    @classmethod
    async def _getbypk(cls: Type[MODEL], key: Any) -> MODEL:
        try:
            return await cls.get(pk=key)
        except (DoesNotExist, ValueError):
            raise KeyError(f"{cls._meta.full_name} has no object {repr(key)}")

Checklist:

abondar commented 3 weeks ago

Hi!

Sorry for confusion - just merged https://github.com/tortoise/tortoise-orm/pull/742 which did basically the same few hours before Still hope your problem is resolved by merged change, I will release it as 0.21.4 a little bit later