I have a method that should return one of three objects depending on their existence.
My implementation
try:
return Model.objects.get(param=param)
except Model.DoesNotExist as ex:
pass
try:
return RelatedModel.objects.get(param=param).model
except RelatedMolel.DoesNotExist as ex:
pass
return Model.objects.get_default()
So, the question for the python guru is a more pythonic implementation than the internal try / catch blocks or?
try:
return Model.objects.get(param=param)
except Model.DoesNotExist as ex:
try:
return RelatedModel.objects.get(param=param).model
except RelatedModel.DoesNotExist as ex:
return Model.objects.get_default()
source
share