How to change the default username to email, in DRF obtain token login

296 views
Skip to first unread message

Suraj Singh

unread,
Aug 11, 2022, 11:33:03 PM8/11/22
to Django REST framework

Abhishek Chauhan

unread,
Aug 11, 2022, 11:34:24 PM8/11/22
to django-res...@googlegroups.com
Use custom user model and override username to email

On Fri, 12 Aug 2022 at 9:03 AM, Suraj Singh <surajsi...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/ce9f65f7-d666-4479-b486-76f2f95f583fn%40googlegroups.com.
--
Abhishek Chauhan

✆ : 9560 432 275





M Adnan

unread,
Aug 12, 2022, 12:19:40 AM8/12/22
to django-res...@googlegroups.com
email = request.data.get('email').lower(
).strip() if 'email' in request.data else None
password = request.data['password'] if 'password' in request.data else None
if not email or not password:
return Response({"success": False, 'response': {'message': 'Invalid data.'}},
status=status.HTTP_400_BAD_REQUEST)
try:
user = User.objects.get(email=email).username
except:
return Response({"success": False, 'response': {'message': 'Incorrect User Credentials!'}},
status=status.HTTP_404_NOT_FOUND)
try:
user = User.objects.get(email=email is_active=True).username
except:
return Response({"success": False, 'response': {'message': 'Sorry! Your account is not active!'}},
status=status.HTTP_400_BAD_REQUEST)
user = authenticate(username=user, password=password)

M Adnan

unread,
Aug 12, 2022, 12:19:58 AM8/12/22
to django-res...@googlegroups.com
try this I hope this will help you

Antun Franjin

unread,
Aug 12, 2022, 3:57:02 AM8/12/22
to django-res...@googlegroups.com
Something like this:

from django.contrib.auth.models import AbstractUser, AbstractBaseUser
from django.db import models
from django.utils.translation import gettext_lazy as _


class CustomUser(AbstractUser):
USERNAME_FIELD = 'email'
email = models.EmailField(_('email address'), unique=True)
REQUIRED_FIELDS = ['username',]

class Meta:
db_table = 'auth_user'


And ofc you need to overirde backend if you want to use custom user:

class CustomBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user = UserModel.objects.get(
Q(username__iexact=username) | Q(email__iexact=username))
except UserModel.DoesNotExist:
UserModel().set_password(password)
except UserModel.UserMultipleObjectsReturned:
return UserModel.objects.filter(email=username).order_by('id').first()
else:
if user.check_password(password) and self.user_can_authenticate(user):
return user

def get_user(self, user_id):
try:
user = UserModel.objects.get(pk=user_id)
except UserModel.DoesNotExist:
return None

return user if self.user_can_authenticate(user) else None

And in settings file:
AUTH_USER_MODEL = 'users.CustomUser'
AUTHENTICATION_BACKENDS = (
'path.to_backends_folder.CustomBackend',
'guardian.backends.ObjectPermissionBackend',
)

I think something like that maybe some stuff you will not need but anyhow.

Best Regards, Antun
Reply all
Reply to author
Forward
0 new messages