DoesNotExist:对象匹配查询不存在

时间:2013-07-27 14:28:27

标签: django postgresql django-models django-views django-south

当我执行“object_name.objects.get(id = 1)”时,我收到错误。之前我也在id = 1时收到此错误,但是当id = 2时它返回了对象。然后我删除了我所做的所有条目,希望它从id = 1开始,但现在它给出了错误。

有人可以解释为什么会这样,我该怎么办?

>>> from course.models import Course, Content

>>> Course.objects.get(id=1)

Traceback (most recent call last):

  File "<console>", line 1, in <module>

  File "/home/vagrant/edu-venv/local/lib/python2.7/site-packages/django/db/models    /manager.py", line 143, in get
    return self.get_query_set().get(*args, **kwargs)

  File "/home/vagrant/edu-venv/local/lib/python2.7/site-packages/django/db/models    /query.py", line 389, in get
    (self.model._meta.object_name, kwargs))

DoesNotExist: Course matching query does not exist. Lookup parameters were {'id': 1}

此外,object_name.object.all()提供了我输入的所有内容

>>> Course.objects.all()
[<Course: JAVA>, <Course: HTML5>]

1 个答案:

答案 0 :(得分:1)

每次创建对象时,数据库都会递增主键。如果使用id=1id=2创建对象,则即使删除现有对象,下一个对象也始终具有id=3