Django'< object>当我可以在数据库中看到它时,匹配查询不存在“ [英] Django '<object> matching query does not exist' when I can see it in the database
问题描述
我的模型看起来像这样:
My model looks like this:
class Staff(models.Model):
StaffNumber = models.CharField(max_length=20,primary_key=True)
NameFirst = models.CharField(max_length=30,blank=True,null=True)
NameLast = models.CharField(max_length=30)
SchoolID = models.CharField(max_length=10,blank=True,null=True)
AutocompleteName = models.CharField(max_length=100, blank=True,null=True)
我正在使用MySQL,万一重要。
I'm using MySQL, in case that matters.
从manage.py shell :
From the manage.py shell:
root@django:/var/www/django-sites/apps# python manage.py shell
Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from disciplineform.models import Staff
>>> s = Staff.objects.all()
>>> len(s)
406
所以我知道那里有406Staff对象。我也可以在数据库中看到它们。我查看一个值:
So I know there are 406 "Staff" objects in there. I can also see them in the database. I check one of the values:
>>> s[0].NameFirst
u'"ANDREA"'
这也匹配我在数据库中查看。现在我试图得到这个对象。
That also matches what I see in the database. Now I try to 'get' this object.
>>> a = Staff.objects.get(NameFirst='ANDREA')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/var/lib/python-support/python2.5/django/db/models/manager.py", line 93, in get
return self.get_query_set().get(*args, **kwargs)
File "/var/lib/python-support/python2.5/django/db/models/query.py", line 309, in get
% self.model._meta.object_name)
DoesNotExist: Staff matching query does not exist.
嗯?对于我测试的所有列的所有值,都会发生这种情况。我在我的view.py代码中得到相同的结果。
Huh? This is happening for all the values of all the columns I've tested. I'm getting the same result in my view.py code.
我显然做的很蠢。这是什么?
I'm obviously doing something dumb. What is it?
推荐答案
尝试
a = Staff.objects.get(NameFirst=u'"ANDREA"')
u
告诉Python / Django它是一个Unicode字符串,不是一个简单的旧的 str
,而在你的 s [0] .NameFirst
示例,它的值显示为双引号。
The u
tells Python/Django it's a Unicode string, not a plain old str
, and in your s[0].NameFirst
sample, it's showing the value as containing double quotes.
这篇关于Django'< object>当我可以在数据库中看到它时,匹配查询不存在“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!