After python upgrade, User.objects.get(username= doesn't work consistently

568 views
Skip to first unread message

Hanne Moa

unread,
May 22, 2012, 4:49:59 PM5/22/12
to django...@googlegroups.com
I upgraded python to 2.6.8 today from an earlier 2.6, what an
adventure! So much bizarre happenings so many places. Tonight's
special:

SomeModel.objects.get(sometextfield=sometext) always works (when there
is one entry where sometextfield=sometext of course).

auth.User.objects.get(username=sometext) works only *sometimes*.

In a login-view, I first check that the user for a given username
exists. It does! User found. Then I try to authenticate:
auth.authenticate(user.username, password). That never works!
Monkeypatching authenticate() shows that the
User.objects.get(username=username) in it always returns
User.DoesNotExist. But of course:
User.objects.filter(username__startswith=username).get(username__endswith=username)
always return the user in question. And the only thing that has
changed is the minor python version.

I can't get User.objects.get(username=whatever) to work in the shell either.

How do I debug this further? This is for django 1.3, I need to have
this working predictably again before I can upgrade to 1.4. I have
logs of the sql if that'll help but the sql looks fine.


HM

Kurtis Mullins

unread,
May 22, 2012, 4:58:51 PM5/22/12
to django...@googlegroups.com
Hey,

Sorry I'm a bit confused so I'm going to try to make some sense of
your situation "out loud" :)

1. User.objects.get(username="some_username") works some-times
2. When it doesn't work, then you get the DoesNotExist exception, right?
3. If not, what error(s) do you see?
4. Are you sure the user, with that username, does exist when you get
the exception?
5. If you are sure they exist and you still get the exception, did you
look into the database itself to see if it exists? Or how did you come
to this conclusion?
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Hanne Moa

unread,
May 22, 2012, 6:28:08 PM5/22/12
to django...@googlegroups.com
On 22 May 2012 22:58, Kurtis Mullins <kurtis....@gmail.com> wrote:
> Sorry I'm a bit confused so I'm going to try to make some sense of
> your situation "out loud" :)
>
> 1. User.objects.get(username="some_username") works some-times

Yes.

> 2. When it doesn't work, then you get the DoesNotExist exception, right?

Yes.

> 3. If not, what error(s) do you see?

I haven't tried it on non-existing users. I except to get a user back.

> 4. Are you sure the user, with that username, does exist when you get
> the exception?

Yes. The user exists if I do an objects.all() or startswith/endswith.
I can do User.objects.get(id=... and get the user I want, i just can't
look em up by username.

> 5. If you are sure they exist and you still get the exception, did you
> look into the database itself to see if it exists? Or how did you come
> to this conclusion?

In a view I run User.objects.gte() by hand, then the user exists, then
a handful of lines later, still in the same view, User.objects.get()
for the same user is run by authenticate() (default ModelBackend) but
then the user does not exist. The database does not change in the
meantime.

The odd thing is it's only the auth.User model that shows this. Other
models are fine.


HM

akaariai

unread,
May 22, 2012, 6:30:40 PM5/22/12
to Django users
Please provide these three things for further debugging:
- the exact Python code you try in the shell (including a real
username).
- the SQL that generates (when not working).
- what does happen when you try to run that SQL in manage.py dbshell
(you might need to add quotes to the parameters)

- Anssi

Hanne Moa

unread,
May 23, 2012, 4:23:49 AM5/23/12
to django...@googlegroups.com
On 23 May 2012 00:30, akaariai <akaa...@gmail.com> wrote:
> On May 22, 11:49 pm, Hanne Moa <hanne....@gmail.com> wrote:
>> I upgraded python to 2.6.8 today from an earlier 2.6, what an
>> adventure! So much bizarre happenings so many places.
>
> Please provide these three things for further debugging:
>  - the exact Python code you try in the shell (including a real
> username).
>  - the SQL that generates (when not working).
>  - what does happen when you try to run that SQL in manage.py dbshell
> (you might need to add quotes to the parameters)

Problem solved. Exact lookups worked only intermittently in the
database as well. Turns out the index on usernames was hosed. I remade
it...

alter table auth_user drop constraint auth_user_username_key;
alter table auth_user add constraint auth_user_username_key UNIQUE (username);

and now it works. For a little while it was possible to make duplicate
users though (!) This calls for champagne, I've never seen an error in
the database that failed so spectacularly silently before =D


HM
Reply all
Reply to author
Forward
0 new messages