peewee-2.6.4
Suppose I have a model named MyModel.
I am trying to fetch a my_model row by using MyModel.get(CONDITION). If such a row can't be found - an exception is raised. The displayed traceback says peewee.MyModelDoesNotExist.
If I try to catch this exception - the result does not change! Diving deeper with a debugger reveals that I should actually catch MyModel.DoesNotExist!
This makes the traceback displayed by peewee confusing and very misleading. Can you please change the traceback message to hint about the correct exception name that should be caught or is there any technical limitation for not doing so?
For anyone like me who stumbles upon this from google with the same question, just use the exception at the top level of the package, and ignore the concatenation of your model name onto the exception in the traceback. For example, for model MyModel with id field id:
import peewee
from my_app.models import MyModel
def get_mymodel(requested_id):
try:
result = MyModel.get(MyModel.id == requested_id)
except peewee.DoesNotExist:
raise ValueError('No instance of MyModel exists at {}'.format(requested_id))
Most helpful comment
For anyone like me who stumbles upon this from google with the same question, just use the exception at the top level of the package, and ignore the concatenation of your model name onto the exception in the traceback. For example, for model
MyModelwith id fieldid: