Найти - Пользователи
Полная версия: как отловить self.model.DoesNotExist? exception DoesNotExist: не раб.
Начало » Django » как отловить self.model.DoesNotExist? exception DoesNotExist: не раб.
1
qman
есть код
>>> dt_value = measuring_datetime.objects.latest('dt_value')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python25\Lib\site-packages\django\db\models\manager.py", line 93, in
latest
return self.get_query_set().latest(*args, **kwargs)
File "C:\Python25\lib\site-packages\django\db\models\query.py", line 306, in l
atest
return self._clone(_limit=1, _order_by=('-'+latest_by,)).get()
File "C:\Python25\lib\site-packages\django\db\models\query.py", line 263, in g
et
raise self.model.DoesNotExist, "%s matching query does not exist." % self.mo
del._meta.object_name
DoesNotExist: measuring_datetime matching query does not exist.
как отловить DoesNotExist?
пробовал
try :
dt_value = measuring_datetime.objects.latest('dt_value')
exception DoesNotExist:
print "DoesNotExist"
[code]

получаю
[code]
NameError at /
global name 'DoesNotExist' is not defined
Request Method: GET
Request URL: http://127.0.0.1:8000/
Exception Type: NameError
Exception Value: global name 'DoesNotExist' is not defined[/code]
как сделать import для данного exception
playpauseandstop
Django documentation
get() raises a DoesNotExist exception if an object wasn’t found for the given parameters. The DoesNotExist exception is an attribute of the model class.
т.е.
try:
page = Page.objects.get(url='/page_url/')
except Page.DoesNotExist:
print "What's a funny url for page? :)"
qman
отработало
try: measuring_datetime.DoesNotExist
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB