Django username in the User model can not be repeated - django

I am trying to create users in the django django.contrib.auth.models.User model but when I use a username which already exists in the database it will give me an error like:
{username: ["A user with that username already exists."]}
as an http response
How can I disable this in django so I can have many users with the same username
Thanks all.

Related

Django rest framework user registration : Update django user database after the email verification is done

I am trying to register the user by sending an OTP to their mail. Consider the following usual:
the user posts a request for registration with an email, password. (email mapped to the username of Django).
Django creates the user and the user gets the OTP for verification. If the user verifies, an 'is_verified' field will be set to true.
Now, If the user doesn't verify himself with the OTP, I can't get around the following issues. Please suggest a workaround.
---> Any other user can now not use the same email for registration, as email already exists in the database.
I want the user to be updated in the database only after the user has successfully verified the otp.

How can I get the username of the logged in user in Django?

I'M new to Django, looking to get first name and last name of the logged-in user in Django,
login method am using LDAP authentication.
when i login as admin i was able to see the LDAP user in User information tab.
Tried request.user.first_name didn't work, is there any other method to get the details?
You can use this method to get first name, last name and all details of the logged in user.
from django.contrib.auth.models import User
first_name = User.objects.get(username=request.user).first_name
last_name = User.objects.get(username=request.user).last_name
In the same way you can get any attribute of the user. For logged in users, you have to use the keyword request.user.
There is two method in AbstractUser class, you can use this
request.user.get_full_name()
request.user.get_short_name()
get_short_name will return first_name

Custom model for authentication in django restframework instead of default user model

I want to do some custom auth for my users with username or email with password. At the first time of logging with email and password the api should return me the respective user token. For all other operations with the api I need to make use of token, which I get at time of login.
And I need a custom model to store all user info like username, password, phone, email, token etc.
How to achieve this in django restframework.
Please guide me to achieve this. Thanks in advance.
Django rest-framework has a built in token system which can be used to distribute and authenticate tokens. Below is a sample of how to use it.
Create TOKEN
from rest_framework.authtoken.models import Token
user = User.objects.get(pk=some_pk)
token = Token.objects.create(user=user)
Authenticate token
if Token.ojects.get(key=token) # token is sent by client side
# do some task as auth is successful
If you want to extend the default User model then create a new model and put a onetoone field in your custom model which references default User model.
class AppUserProfile(models.Model):
user = models.OneToOneField(User)
... # add other custom fields like address or phone

multiple devise models with multiple login pages

I created three devise models user, admin, trainer. For user and admin i want login with email and password. For trainer I want login with username and password. How we can achieve this ?
Your Users and Admin should already be logging in with email and password by default with Devise.
To set up login with username for your Trainers checkout this link.
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address
This will allow your Trainers to login with either username or email. You can display either on the pages because we are adding a username migration to the trainers model.
You will also need to add field to the Trainer signup page.

Issues with auth_user table in django

I want to update some fields of auth_user table in django. Actually i am migrating some users from one website to another so i want to update the password field in auth_user table.But when i am using the update query it gives me some errors
some things which i have tried
values=User.objects.get(username=request.POST['username'])
values.password=request.POST['password']
values.password.save()
it gives the error of 'unicode' object has no attribute 'save
and if i tried this one
values=User.objects.get(username=request.POST['username']).update(password=request.POST['password'])
then the error is 'User' object has no attribute 'update'
actually i do not want to send emails to users to update their password and redirect them to forgot password page.
But whenever user try to login to site and if his password do not match but he typed the password correctly but due to migration his password do not work in django then the password he enters must be updated in auth_user table(encrypted password).
In between i have ensure that this user is the authenticate user of previous site.
So please suggest me some way so that i can update his password in auth_user table.
Passwords in django are stored as sha256 hashes, so setting
user.password = 'new password'
is not a good idea. Fortunately django has methods that would take care of hashing your password. Second thing:
values.password.save()
Here you are trying to execute save() method on password object which is a string, not a user object. values.save() would be better, but still not correct. What you want to do is this:
values.set_password('new password') # Takes care of hashing
values.save()
More on the topic in django documentation
(On behalf of OP)
I used this and the problem is solved
u = User.objects.get(username__exact='john')
u.set_password('new password')
u.save()