How to check if user is already in user database?

811 views
Skip to first unread message

joulumaa

unread,
Jan 16, 2015, 4:00:32 PM1/16/15
to django...@googlegroups.com
Hi,
  
If  user = auth.authenticate(username,password) results None, I would like to create new user and login it.
But I cannot find a way to determine easily if it is was None because of 1) it was not in database or 2) it was in database but password was wrong
I addition to those it may have been inActive or without permission, but first thing to check would be to find if username is in user database.
Is there any fast way to check it?

-thanks for help

Shazwi Suwandi

unread,
Jan 16, 2015, 4:25:29 PM1/16/15
to django...@googlegroups.com
Have you tried something like user = User.objects.get(username="username")

joulumaa

unread,
Jan 16, 2015, 5:10:37 PM1/16/15
to django...@googlegroups.com
so, if it was not in database is result None, or some err or how to validate? sorry I am very beginner ;-)

then same problem with that permission, how to verify if reason for failing was that?

-thanks

James Bennett

unread,
Jan 16, 2015, 5:12:08 PM1/16/15
to django...@googlegroups.com
This may be a good time to review Django's documentation on how to perform database queries:

https://docs.djangoproject.com/en/1.7/topics/db/queries/#retrieving-a-single-object-with-get

Mark Furbee

unread,
Jan 16, 2015, 5:18:06 PM1/16/15
to django...@googlegroups.com
Yes, that link will cover the basics...

Here's a couple ways to find out if a query returns any records:

    User.objects.filter(username=username).exists()  # .exists() returns boolean if records are found.
    User.objects.filter(username=username).count() != 0  # .count() returns number of records in query.
    User.objects.filter(username=username).first() is not None # .first() returns the first record if there are matches, and None if there aren't
    

On Fri, Jan 16, 2015 at 9:11 AM, James Bennett <ubern...@gmail.com> wrote:
This may be a good time to review Django's documentation on how to perform database queries:

https://docs.djangoproject.com/en/1.7/topics/db/queries/#retrieving-a-single-object-with-get

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAL13Cg8MaAhTh%2B%3DoGTJ75jeZVY8-gt_hMkUTjZxyLxFkQ%2Bx36Q%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Mark Furbee

unread,
Jan 16, 2015, 5:19:09 PM1/16/15
to django...@googlegroups.com
Also, you can do a try/catch, but I don't really like this method, personally:

    try:
        User.objects.get(username=username)
    except User.DoesNotExist:
        # user didn't exist in database.

joulumaa

unread,
Jan 16, 2015, 5:54:44 PM1/16/15
to django...@googlegroups.com
Exception Type: TypeError
Exception Value:
catching classes that do not inherit from BaseException is not allowed

Did not work.
Get might be faster than filter, but .exact() did not work either

-thanks

joulumaa

unread,
Jan 16, 2015, 5:56:04 PM1/16/15
to django...@googlegroups.com
ah...not exact but exist()....
Reply all
Reply to author
Forward
0 new messages