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

This is my login path

from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
     ...
    path('login/', obtain_auth_token), 
]

When I use this as a login, it takes username(though it takes email as field data) & password and generate the token for me

{
    "username": "manig@gmail.com",
    "password" : "manig@gmail.com"
}

how I want it


{
    "email": "manig@gmail.com", 
    "password" : "manig@gmail.com"
}

I want to replace username with email!!
how should I modify this ?

**What I have tried till now is: **
I have made the custom serializer

class AuthTokenSerializer(serializers.ModelSerializer):
    class Meta:
        model = AuthUser
        fields = ('email', 'password')
        extra_kwargs = {'password': {'write_only': True}}
    
    def validate(self, data):
        user_obj = None
        email = data.get('email')
        password = data.get('password')
        if email and password:
            user_obj = AuthUser.objects.filter(email=email).first()
            if not user_obj:
                raise serializers.ValidationError("This email is not registered")
            if not user_obj.check_password(password):
                raise serializers.ValidationError("Incorrect credentials")
        return data
    
    def create(self, validated_data):
        user = AuthUser.objects.get(email=validated_data['email'])
        refresh = RefreshToken.for_user(user)
        return {
            'user': user,
            'token': str(refresh.access_token)
        }

And here is the view for it

class AuthLoginUser(ObtainAuthToken):
    def post(self, request, *args, **kwargs):
        serializer = AuthTokenSerializer(data=request.data, context={'request': request})
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        token, created = Token.objects.get_or_create(user=user)
        return Response({
            'token': token.key,
            'first_name': user.first_name,
            'last_name': user.last_name,
            'email': user.email,
            'role': user.role
            })

And this is how url pattern is

urlpatterns = [
    ...
    path('login/', views.AuthLoginUser.as_view()),
] 

And when I do this it does replaces username field with email, but when I login/pass the data through the endpoint

It starts giving me this error, user with this email already exists, instead it should have replied with token, first name, lastname email and role

How can I fix this issue ?

Hey @Suraj7879
Remember DRF “abstracts” whats write in Django code, for putting in that way.
You need to create an “AbstractUser”, which is a partially-custom User model
You’d need to do something like this:

class CustomUser(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

Then you need to call this “CustomUser” model in your “settings.py” file, like this:

AUTH_USER_MODEL = 'nameOfTheApp.CustomUser'

This would be the good way of doing it.
The “bad” way could be to change the “username” field for a “models.EmailField”, but the field required for DRF would be still “username” instead of “email”
Let me know if this helps you, or I can help you in any other way
Greetings