how to get User id from auth_user
table in django. Suppose username is availlable to me.
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.
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.
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 ...
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.
Assuming the user exists:
from django.contrib.auth.models import User
User.objects.get(username=the_username).pk
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With