Peewee: Confused about inconsistency of *DoesNotExist exceptions

Created on 28 Sep 2015  路  1Comment  路  Source: coleifer/peewee

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?

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 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))

>All comments

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))
Was this page helpful?
0 / 5 - 0 ratings

Related issues

megachweng picture megachweng  路  3Comments

kadnan picture kadnan  路  3Comments

mikemill picture mikemill  路  3Comments

vkrizan picture vkrizan  路  3Comments

mmongeon-aa picture mmongeon-aa  路  4Comments