Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which file is used in import statement for 'MultipleObjectsReturne' exception

I am using this code

except MultipleObjectsReturned:
    return HttpResponse('some error')

but i get this error

global name 'MultipleObjectsReturned' is not defined

like image 215
user1 Avatar asked Aug 04 '26 08:08

user1


2 Answers

You can do either:

from django.core.exceptions import MultipleObjectsReturned

except MultipleObjectsReturned as e:
    return HttpResponse(e)

Or:

except yourmodel.MultipleObjectsReturned as e:
    return HttpResponse(e)

https://docs.djangoproject.com/en/1.3/ref/exceptions/#django.core.exceptions.MultipleObjectsReturned

A base version of this exception is provided in django.core.exceptions; each model class contains a subclassed version that can be used to identify the specific object type that has returned multiple objects.

like image 88
mechanical_meat Avatar answered Aug 06 '26 20:08

mechanical_meat


from django.core.exceptions import MultipleObjectsReturned
like image 36
Botond Béres Avatar answered Aug 06 '26 22:08

Botond Béres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!