auth system - check if user already exists

617 views
Skip to first unread message

Aljosa Mohorovic

unread,
Aug 5, 2007, 5:42:38 PM8/5/07
to Django users
i'm currently looking at django.contrib.auth and wondering what is the
correct way to check if user already exists?
before i call User.objects.create_user() i want to know that user with
submitted username and email doesn't exist.
my current situation is that i call
User.objects.get(username__exact=user) and
User.objects.get(email__exact=email) and if i get exception
"DoesNotExist: User matching query does not exist." and i assume that
it's ok to create new user.

i'm sure there is a better way to check if user with submitted
username or email already exists, please share your ways...

Aljosa

Poromenos

unread,
Aug 5, 2007, 7:29:47 PM8/5/07
to Django users
That's what I do (well, I just check the username, since I can't think
why you'd want to check the email). I don't think there's a better
way, it's pretty straightforward...

Poromenos

On Aug 6, 12:42 am, Aljosa Mohorovic <aljosa.mohoro...@gmail.com>
wrote:

LaundroMat

unread,
Aug 6, 2007, 4:35:40 AM8/6/07
to Django users
Of the top of my head, but would the following be helpful?
user, new = User.objects.get_or_create(username = user, email =
email)

The function returns either:
- a new user object and True if the user didn't exist yet (and had to
be created);
- an object with existing user data and False.

Mind that you still have to call user.save() in case of a new user.

On Aug 5, 11:42 pm, Aljosa Mohorovic <aljosa.mohoro...@gmail.com>
wrote:

Aljosa Mohorovic

unread,
Aug 6, 2007, 6:02:55 AM8/6/07
to Django users
i'm checking username and email because i want to avoid situations
where some user registers with same email using different username.

Aljosa

LaundroMat

unread,
Aug 6, 2007, 10:55:57 AM8/6/07
to Django users
Are you sure you want to avoid that? I remember somebody here linking
to an article where this wasn't advised, because it's (apparently)
quite common for members of the same household to register with the
same e-mail address (but with another username ofcourse).

On Aug 6, 12:02 pm, Aljosa Mohorovic <aljosa.mohoro...@gmail.com>
wrote:

Aljosa Mohorovic

unread,
Aug 6, 2007, 12:49:33 PM8/6/07
to Django users
On Aug 6, 4:55 pm, LaundroMat <Laun...@gmail.com> wrote:
> Are you sure you want to avoid that? I remember somebody here linking
> to an article where this wasn't advised, because it's (apparently)
> quite common for members of the same household to register with the
> same e-mail address (but with another username ofcourse).

good point but in my situation users will mostly be companies so i
want to ensure that nobody with same email registers twice.

Aljosa

Reply all
Reply to author
Forward
0 new messages