Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get User id from auth_user table in django?

Tags:

python

django

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

like image 889
curiousguy Avatar asked Feb 23 '13 19:02

curiousguy


People also ask

How can I get current ID in Django?

First make sure you have SessionMiddleware and AuthenticationMiddleware middlewares added to your MIDDLEWARE_CLASSES setting. request. user will give you a User object representing the currently logged-in user. If a user isn't currently logged in, request.

How do I get all Django usernames?

auth import get_user_model, allows us to import the User class. This imports the class that contains all of the users in the Django default user model.

How do I find my Django username and password?

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...

What is user Is_active in Django?

is_active. Boolean. Designates whether this user account should be considered active. We recommend that you set this flag to False instead of deleting accounts; that way, if your applications have any foreign keys to users, the foreign keys won't break.


2 Answers

Assuming the user exists:

from django.contrib.auth.models import User

User.objects.get(username=the_username).pk
like image 79
Pavel Anossov Avatar answered Sep 23 '22 19:09

Pavel Anossov


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

like image 40
boskicthebrain Avatar answered Sep 23 '22 19:09

boskicthebrain