curiousguy
curiousguy

Reputation: 3262

how to get User id from auth_user table in django?

how to get User id from auth_user table in django. Suppose username is availlable to me.

Upvotes: 6

Views: 16182

Answers (2)

boskicthebrain
boskicthebrain

Reputation: 545

User doesn't have backend so you have to use a UserManager (or BaseUserManager if you have custom User class)

BaseUserManager.get_by_natural_key(username)

https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#django.contrib.auth.models.BaseUserManager

Upvotes: 1

Pavel Anossov
Pavel Anossov

Reputation: 62908

Assuming the user exists:

from django.contrib.auth.models import User

User.objects.get(username=the_username).pk

Upvotes: 19

Related Questions