I am using this code
except MultipleObjectsReturned:
return HttpResponse('some error')
开发者_如何学Python
but i get this error
global name 'MultipleObjectsReturned' is not defined
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.
from django.core.exceptions import MultipleObjectsReturned
精彩评论