-1

I have to use a try and except block with the following code, as I am trying to get a model class object but in case if database is empty so for that I need to use try and except.

if(txStatus=='SUCCESS'):
    order=Order.objects.get(id=id)    #NEED TRY ACCEPT BLOCK FOR THIS
    URL = payment_collection_webhook_url 
    request_data ={}          
    json_data = json.dumps(request_data)
    requests.post(url = URL, data = json_data)
    return Response(status=status.HTTP_200_OK)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Amit Yadav
  • 29
  • 1
  • 9
  • Exactly what should be done if the item is missing in the database? – Willem Van Onsem May 20 '21 at 17:19
  • There are convenience functions to handle a missing object. They start with `get_or_...`. – Klaus D. May 20 '21 at 17:47
  • 1
    Does this answer your question? [How do I get the object if it exists, or None if it does not exist?](https://stackoverflow.com/questions/3090302/how-do-i-get-the-object-if-it-exists-or-none-if-it-does-not-exist) – Ankit Tiwari May 20 '21 at 18:08

3 Answers3

1

It is as simple as this:

try:
    order = Order.objects.get(id=order_id)
except Order.DoesNotExist:
    # Exception thrown when the .get() function does not find any item.
    pass  # Handle the exception here. 

You can find more information about DoesNotExist exception here.

Hagyn
  • 922
  • 7
  • 14
1

try..except..else..finally block works like this:

try:
    order=Order.objects.get(id=id)
except ObjectDoesNotExist(or MultipleObjectsReturned in case it returns more 
than 1 queryset or instance):
    ...Handle the exception, Write any code you want
else:
    ...This gets called if there is no exception or error, means it will 
    execute right after try statement if there's no error, so if you want 
    something more to happen only if the code doesn't throw an error, you can 
    write it here
finally:
    ...This gets executed no matter what in any case, means if there's 
    something you want to execute regardless of whether it throws an 
    exception or not, you write it here.
  • Thanks a lot @HardeepChhabra you explained it really well. Can u please explain with an example in the part where u have written 'Handle the exception, Write any code you want' this block of code will only execute if object does not exists right? – Amit Yadav May 20 '21 at 18:05
  • 1
    hello @AmitYadav yes it will work when the exception occur one suggestion i would like to give you can use `except Exception as e:` if you don't know which exception is going to come and you can also use `get_object_or_404(Order,id=id)` if record does not exists then it will return `404` – Ankit Tiwari May 20 '21 at 18:14
  • Thanks a lot @AnkitTiwari for the suggestion. – Amit Yadav May 20 '21 at 18:20
  • Oky thanks a lot @HardeepChhabra for the help – Amit Yadav May 22 '21 at 08:29
  • 1
    @AmitYadav Sorry to reply you so late. And yes, the EXCEPT block will only execute whenever Django throws an error. For example, in your case, whenever the object doesn't exist, it will go into EXCEPT block, why ? Because Django throws error everytime there's no object in the model. So, long story short, it goes into **try** first, no matter what. If Django throws an error, it will immediately jump into **except**, and then there you can handle the error however you want, then it goes into **finally**. If there's no error, then **try**,**else**,**finally**. Simple. Please Upvote if this helped – Hardeep Chhabra May 22 '21 at 08:39
0

@Hagyn is correct, but in Django there is another way to do that:

Something like this:

orders = Order.objects.filter(id=order_id)
if orders.exists():
    order = orders.last()
else:
    # do rest of the things
Ashraful Islam
  • 546
  • 2
  • 8