1

Is there a possibility to return none if no results are found by a object.get() function call? I know it throws an exception by default, but I want to return none from it.

Abdul Rehman
  • 2,313
  • 2
  • 15
  • 14

1 Answers1

4

You can do it with exceptions like:

 try:    
     object = Model.objects.get(foo='bar') 
 except Model.DoesNotExist:    
     object = None
Aamish Baloch
  • 1,200
  • 12
  • 9