43👍
✅
As Chris says in the comments above, your snippet is valid. Somewhere else in your code, you may be catching exceptions incorrectly.
You may have something like:
try:
do_something()
except User.MultipleObjectsReturned, User.DoesNotExist:
pass
instead of:
try:
do_something()
except (User.MultipleObjectsReturned, User.DoesNotExist):
pass
Without the parentheses, the except statement is equivalent to the following in Python 2.6+
except User.MultipleObjectsReturned as User.DoesNotExist:
The instance of the User.MultipleObjectsReturned
exception overwrites User.DoesNotExist
.
When the same process handles a different request later on, you get
the TypeError
because your code is trying to call the exception instance which has replaced User.DoesNotExist
.
Source:stackexchange.com