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

pet, 12. kol 2022. u 06:19 M Adnan <[email protected]> napisao je:

> try this I hope this will help you
>
>
> On Fri, Aug 12, 2022 at 9:19 AM M Adnan <[email protected]> wrote:
>
>> 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)
>>
>> On Fri, 12 Aug 2022, 8:34 am Abhishek Chauhan, <[email protected]> wrote:
>>
>>> Use custom user model and override username to email
>>>
>>> On Fri, 12 Aug 2022 at 9:03 AM, Suraj Singh <[email protected]>
>>> wrote:
>>>
>>>> Kindly help me to solve this issue, posted it in django forum,
>>>>
>>>>
>>>> https://forum.djangoproject.com/t/how-to-change-the-default-username-to-email-in-drf-obtain-token-login/15383
>>>>
>>>> --
>>>> 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 [email protected].
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/django-rest-framework/ce9f65f7-d666-4479-b486-76f2f95f583fn%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/django-rest-framework/ce9f65f7-d666-4479-b486-76f2f95f583fn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> --
>>> *Abhishek Chauhan*
>>>
>>> ✆ : 9560 432 275
>>>
>>>  *@abhiazh* <https://twitter.com/abhiazh>
>>>
>>>
>>>
>>>
>>> --
>>> 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 [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-rest-framework/CAOaWsMFR8nqvzVpiq9L153hvqmW9X5Z6WVuVfq%2BjnPVAkVP_Bw%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/django-rest-framework/CAOaWsMFR8nqvzVpiq9L153hvqmW9X5Z6WVuVfq%2BjnPVAkVP_Bw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/CABNyTSr%3DgVfXF2U%3DmHeOUstYZGABsvNhEwk%3DspHdJPDkoULEoQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-rest-framework/CABNyTSr%3DgVfXF2U%3DmHeOUstYZGABsvNhEwk%3DspHdJPDkoULEoQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CAAKMy4LGCcNYw5NivRqSdQQuBgnxwVUd_N_PPLM5jbYeL6jvDA%40mail.gmail.com.

Reply via email to