更通用的实现django orm查找记录不存在的except
2021-01-04 本文已影响0人
万州客
两种except,一种要导入,一种要写model。主要看个人习惯。
参考URL:
https://blog.csdn.net/dqchouyang/article/details/78246598
一,导入ObjectDoesNotExist
导入from django.core.exceptions import ObjectDoesNotExist,我感觉好点。
import os
import django
from django.core.exceptions import ObjectDoesNotExist
try:
site_item = Site.objects.get(name=site)
except ObjectDoesNotExist as e:
print(e)
二,和model关联DoesNotExist
不导入,但要和model绑定
try:
site_item = Site.objects.get(name=site)
except Site.DoesNotExist as e:
print(e)
随便pick!