我对Django ORM有一个奇怪的问题。
这里的Action
对象的ID为1和6,但不能先获取id。
它们都存在于数据库中。
In [17]: Action.objects.get(code='check_email').pk
Out[17]: 6L
In [18]: Action.objects.get(code='periodic_order_check').pk
Out[18]: 1L
In [19]: Action.objects.get(pk='6')
Out[19]: <Action: Check Email>
In [20]: Action.objects.get(pk='1')
---------------------------------------------------------------------------
DoesNotExist Traceback (most recent call last)
<ipython-input-20-728196789a45> in <module>()
----> 1 Action.objects.get(pk='1')
/home/vagrant/.pyenv/versions/vagrant/lib/python2.7/site-packages/django/db/models/manager.pyc in manager_method(self, *args, **kwargs)
120 def create_method(name, method):
121 def manager_method(self, *args, **kwargs):
--> 122 return getattr(self.get_queryset(), name)(*args, **kwargs)
123 manager_method.__name__ = method.__name__
124 manager_method.__doc__ = method.__doc__
/home/vagrant/.pyenv/versions/vagrant/lib/python2.7/site-packages/cacheops/query.pyc in get(self, *args, **kwargs)
351 qs = self
352
--> 353 return qs._no_monkey.get(qs, *args, **kwargs)
354
355 def exists(self):
/home/vagrant/.pyenv/versions/vagrant/lib/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
385 raise self.model.DoesNotExist(
386 "%s matching query does not exist." %
--> 387 self.model._meta.object_name
388 )
389 raise self.model.MultipleObjectsReturned(
DoesNotExist: Action matching query does not exist.
答案 0 :(得分:0)
我建议您使用
Action.objects.all().values()
检查所有值,看看你的get查询是否正确。由于您使用的字段是pk(前提是它是主键,而您尚未重新定义它),因此传递字符串&#39; 1&#39;反正不应该工作。检查模型和字段类型以及所有字段的值。