3π
A bare except:
will catch anything and everything, so it makes no sense to have a second except block in this case.
Possibly you wanted to put the code inside your first except
block into another nested try/except block.
Note: Pokemon exception handling is considered bad coding style, and itβs better if you only try to catch the actual exceptions which you intend to handle β in this case, only catching the DoesNotExist
should be sufficient.
You might consider using a loop to refactor this:
PostModels = {
'postD': PostD,
'postY': PostY,
'postR': PostR,
}
for k,Post in PostModels.items():
try:
post = Post.objects.get(pk=pk)
except Post.DoesNotExist:
pass
else:
return PostReply.objects.filter(k=post)
else:
# all 3 lookups failed, how do you want to handle this?
1π
Since the first except
will catch any exception raised from the try
block, the only way βthe above try and except statements failβ is if code in the first except
block raises an exception. To catch that, you should wrap it directly with try/except
:
def get_queryset(self)
try:
...
except:
try:
<<original first except-block here>>>
except:
<<original second except-block here>>>
Also, in general you should avoid bare except:
.
- [Django]-Django filename from database with non-ascii characters
- [Django]-Django and post request to an external API in different views
- [Django]-Can't add object to Django's Many-To-Many Field
- [Django]-Django object deletion does not work
- [Django]-Where's the primary key of the object I want to update in django using modelform?