9

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)

I'm using MySQL, in case that matters.

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

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.

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?

5 Answers 5

8

Try

a = Staff.objects.get(NameFirst=u'"ANDREA"')

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It was the quotes! That was the dumb thing I was doing. All the values in the database were surrounded by double quotes. I don't even see quotes anymore so I didn't notice. For the record, I tried including the quotes without the Unicode specification and it still worked. Thanks!
You're welcome. I didn't really think it was the Unicode issue, since Django has been uniformly awesome about Unicode handling, but I don't use MySQL so I wanted to make sure all my bases were covered.
In my case it was a variable so I used unicode(variable_name).
2

I ran into the same issue, here's the solution:

from django.db import reset_queries, close_connection
close_connection()
reset_queries()

Comments

2

Name was stored in database with an extra redundant doublequotes. So, if you want to catch that record, correct code is:

a = Staff.objects.get(NameFirst='"ANDREA"')

…instead of:

a = Staff.objects.get(NameFirst='ANDREA')

If you can't be sure, that query will return result, you must to add exception handling, something like that:

try:
    a = Staff.objects.get(NameFirst='"ANDREA"')
except Exception:
    a = None

Comments

0

I have run into similar issues before.

I'm not entirely sure why but the raw 'get' tends to give me problems. So, I usually end up using 'filter' instead, then grabbing the first result.

a = Staff.objects.filter(NameFirst='ANDREA')
result = a[0]

Comments

-2

A simliar thing happened to me. I was able to solve it by putting something in the db for the query to return.

This isn't exactly like yours, but it was the first hit on Google, so I thought I'd share.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.