如标题所示,如果它没有提出404,我有任何方法可以告诉它吗?
答案 0 :(得分:0)
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#get-object-or-404
注意:与get()一样,如果找到多个对象,将引发MultipleObjectsReturned异常。
编辑:
def my_view(request):
try:
my_object = get_object_or_404(MyModel, something=1)
except MyModel.MultipleObjectsReturned:
# Maybe do something.
raise Http404