MultipleObjectsReturned

MultipleObjectsReturned

我在用这个密码

except MultipleObjectsReturned:
    return HttpResponse('some error')

但我知道这个错误
global name 'MultipleObjectsReturned' is not defined

最佳答案

您可以执行以下任一操作:

from django.core.exceptions import MultipleObjectsReturned

except MultipleObjectsReturned as e:
    return HttpResponse(e)

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

https://docs.djangoproject.com/en/1.3/ref/exceptions/#django.core.exceptions.MultipleObjectsReturned
此异常的基本版本是
django.core.exceptions中提供;
每个模型类都包含一个子类
可用于识别
具有
返回多个对象。

08-08 06:58