我需要检查表中是否存在对象,如果没有引发错误,则将其自动保存到django cronjoblog
表中。
这是部分执行我想要的代码,但是它不会引发错误,而是将其保存到cronjoblog表中:
from django.core.exceptions import ObjectDoesNotExist
some_object= Some_object.objects.filter(active=True)
try:
some_object.get()
except ObjectDoesNotExist:
print("Either the entry or blog doesn't exist.")
我需要类似的东西:
some_object= Some_object.objects.filter(active=True)
if not some_object:
raise ObjectDoesNotExist("Either the entry or blog doesn't exist.")
答案 0 :(得分:2)
您可以如下修改现有代码。
try:
some_object.get()
except ObjectDoesNotExist as e:
raise ObjectDoesNotExist("Either the entry or blog doesn't exist.") from e