0

我正在编写我的views.py,我需要在其中通过以下代码获取模型实例:

model_instance = AlertCount.objects.get(user=request.user.username)

但在第一次运行之后python manage.py syncdb。table 的字段最初是empty. 所以它显示了这个错误

DoesNotExist: AlertCount matching query does not exist

这应该是显而易见的。0但是根据我的views.py方法,如果发生此错误,事件将在将所有字段传递给此模型实例后继续执行该方法。

4

1 回答 1

2

如果我理解正确,你想做这样的事情:

try:
  model_instance = AlertCount.objects.get(user=request.user.username)
  # Set your local fields here
except AlertCount.DoesNotExist:
  #Set your local fields here to zero
于 2012-06-30T12:52:29.843 回答