西宁建站,赣榆网站建设xxiaoseo,编辑网站教程,甘肃建设银行网站在django项目中#xff0c;经常使用类似Model.objects.get(id1)的方法取对象#xff0c;默认抛出的异常是ObjectDoesNotExist类型#xff0c;通过try catch可以把异常捕获#xff0c;获取的异常是Model.DoesNotExist类型#xff0c; 要获知其类名#xff0c;可以使用__na…在django项目中经常使用类似Model.objects.get(id1)的方法取对象默认抛出的异常是ObjectDoesNotExist类型通过try catch可以把异常捕获获取的异常是Model.DoesNotExist类型 要获知其类名可以使用__name__方法 要获知其完整类名可以使用__qualname__方法 还想要获取定义类的路径可以使用__module__方法
参考文章 https://blog.csdn.net/NeverLate_gogogo/article/details/107519919
from django.core.exceptions import ObjectDoesNotExist
from variable.models import Variable
aObjectDoesNotExist(变量不存在)
bVariable.DoesNotExist(变量不存在)
try:Variable.objects.get(id0)
except ObjectDoesNotExist as e:c eIn[3]: a.__class__
Out[3]: django.core.exceptions.ObjectDoesNotExist
In[4]: b.__class__
Out[4]: variable.models.Variable.DoesNotExist
In[5]: c.__class__
Out[5]: variable.models.Variable.DoesNotExist
In[6]: a.__class__.__name__
Out[6]: ObjectDoesNotExist
In[7]: a.__class__.__qualname__
Out[7]: ObjectDoesNotExist
In[8]: a.__class__.__module__
Out[8]: django.core.exceptions
In[9]: b.__class__.__name__
Out[9]: DoesNotExist
In[10]: b.__class__.__qualname__
Out[10]: Variable.DoesNotExist
In[11]: b.__class__.__module__
Out[11]: variable.models