Abdul Rehman Abdul Rehman - 1 year ago 94
Python Question

Object.get() throws exception if Nothing is found

Is there a possibility to return none if no results are found by a

function call?
I know it throws an exception by default, but I want to return none from it.

Answer Source

You can do it with exceptions like:

     object = Model.objects.get(foo='bar') 
 except Model.DoesNotExist:    
     object = None
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download