This is my current code:
def get_queryset(self)
pk = self.kwargs['pk']
try:
postD = PostD.objects.get(pk=pk)
# In the line below, PostReply.objects.filter(postD=postD) is
# not guaranteed to exist, so I am using a try statement.
return PostReply.objects.filter(postD=postD)
except:
postY = PostY.objects.get(pk=pk)
# In the line below, PostReply.objects.filter(postY=postY) is
# not guaranteed to exist either, so this may raise an error.
return PostReply.objects.filter(postY=postY)
# Here, I want one last except statement to execute if the above try
# and except statements fail... Is it okay to just add another except
# statement like this:
except:
postR = PostR.objects.get(pk=pk)
# If the above try and except statement fail, this is guaranteed to
# not throw an error.
return PostReply.objects.filter(postR=postR)
I know I can do something like this:
try:
# code
except ObjectDoesNotExist:
# code
except:
# last part of code
but it is not guaranteed that I will be getting an ObjectDoesNotExist
error (I'm not guaranteed what error I will be getting). So I'm wondering if there is a way of having more than one except statement without specifying what exception to look for? Is the way I did it above (where I just have try: except: except:
okay to use?