# Why is username required when I set email as username

**URL:** <https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886>\
**Category:** Forms & APIs\
**Created:** [November 8, 2022, 11:12am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886 "2022-11-08T11:12:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![calcanisa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calcanisa/32/10066_2.png) [@calcanisa](https://forum.djangoproject.com/u/calcanisa)\
**Post date:** [November 8, 2022, 11:12am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/1 "2022-11-08T11:12:10Z")

</div>

I have a Vue front end and Django backend

I am using a custom user model:

```auto
from django.utils import timezone
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager

today = timezone.now()

class UserManager(BaseUserManager):
    def create_user(self, email, password, username=None, role=None):
        user = self.model(email=self.normalize_email(email))
        user.set_password(password)
        user.created_at = today
        user.save()
        return user

    def create_superuser(self, email, password, role, username=None):
        user = self.model(email=self.normalize_email(email))
        user.set_password(password)
        user.role = role
        user.is_staff = True 
        user.is_active = True
        user.is_superuser = True
        user.save()
        return user

class User(AbstractBaseUser, PermissionsMixin):
    EM = 'EM'
    SM = 'SM'
    DH = 'DH'
    ROLES = [
        (EM, 'Executive Management'),
        (SM, 'Senior Management'),
        (DH, 'Department Head'),
      ]
    
    objects = UserManager()
    role = models.CharField(max_length=2, choices=ROLES, default=US, blank=True)
    username = models.CharField(max_length=100, unique=True, blank=True, null=True)
    email = models.EmailField(max_length=255, unique=True)
    slug = models.SlugField()
    is_active = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    email_verified_at = models.DateTimeField(auto_now=False, null=True, blank=True)
    confirmed = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="Created at")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="Updated at")

    class Meta:
        verbose_name = "User"
        verbose_name_plural = "Users"
        ordering = ["username"]
        db_table = "users"

    def get_absolute_url(self):
        return f"{self.slug}"

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['role']
```

I am trying to get an authentication token but the response I get is username is required. Why is it required if I set the username field to email?

I am using this code top get the token:

```auto
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response

class CustomAuthToken(ObtainAuthToken):

    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(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,
            'user_id': user.pk,
            'email': user.email
        })
```

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [November 8, 2022, 11:22am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/2 "2022-11-08T11:22:09Z")

</div>

Hey there!

Are you’re even using the `username` field? If you don’t want the `username` field, you can just set it to `None` on the class, and then set `USERNAME_FIELD = "email"` on the class as well.

This is not related, but this line may not work as you expect:

> [@calcanisa](#):
>
> `today = timezone.now()`

You’re using this variable value inside your functions, and this value is only calculated once, when your server starts and imports this module. You should either:

- move this variable to the function that uses it;
- add the `auto_now_add` on the `created_at` field from your `User` model

---

<div class="post-metadata">

**Author:** ![calcanisa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calcanisa/32/10066_2.png) [@calcanisa](https://forum.djangoproject.com/u/calcanisa)\
**Post date:** [November 8, 2022, 11:30am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/3 "2022-11-08T11:30:36Z")

</div>

I want to use the username field later. I set USERNAME\_FIELD to email in my code

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [November 8, 2022, 11:31am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/4 "2022-11-08T11:31:21Z")

</div>

Ok, does that solves the issue?

---

<div class="post-metadata">

**Author:** ![calcanisa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calcanisa/32/10066_2.png) [@calcanisa](https://forum.djangoproject.com/u/calcanisa)\
**Post date:** [November 8, 2022, 11:32am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/5 "2022-11-08T11:32:17Z")

</div>

no because I had it like that from the start

This part is the cause of the headache

The `obtain_auth_token` view will return a JSON response when valid `username` and `password` fields are POSTed to the view using form data or JSON:

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [November 8, 2022, 11:33am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/6 "2022-11-08T11:33:18Z")

</div>

Alright, can you post the traceback here then?

---

<div class="post-metadata">

**Author:** ![calcanisa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calcanisa/32/10066_2.png) [@calcanisa](https://forum.djangoproject.com/u/calcanisa)\
**Post date:** [November 8, 2022, 11:34am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/7 "2022-11-08T11:34:25Z")

</div>

There is no traceback sir I am using a Vue front end calling the api

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [November 8, 2022, 11:44am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/8 "2022-11-08T11:44:36Z")

</div>

Ok, You’re probably receiving a `400 BAD REQUEST` on your frontend.  
It looks like you’re using the `serializer_class` from the `ObtainAuthToken` view.

> [@calcanisa](#):
>
> ```auto
> class CustomAuthToken(ObtainAuthToken):
> 
> def post(self, request, *args, **kwargs):
> serializer = self.serializer_class(data=request.data,
> context={'request': request})
> serializer.is_valid(raise_exception=True)
> 
> ```

Looking at the source code of the `AuthTokenSerializer` it expects a `username` field.  
How does your POST data looks like?

---

<div class="post-metadata">

**Author:** ![calcanisa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calcanisa/32/10066_2.png) [@calcanisa](https://forum.djangoproject.com/u/calcanisa)\
**Post date:** [November 8, 2022, 11:51am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/9 "2022-11-08T11:51:28Z")

</div>

I am sending an email and password. I know it’s expecting a username and that is my whole problem. How do I change it so it asks for an email. I thought that is what I did when I put in the model that the username must be the email.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [November 8, 2022, 11:53am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/10 "2022-11-08T11:53:56Z")

</div>

You can define a custom `serializer_class` on your view that inherits from the `AuthTokenSerializer` and sets the `source` attribute on the `username` field to `email`.

---

<div class="post-metadata">

**Author:** ![calcanisa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calcanisa/32/10066_2.png) [@calcanisa](https://forum.djangoproject.com/u/calcanisa)\
**Post date:** [November 8, 2022, 11:56am UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/11 "2022-11-08T11:56:01Z")

</div>

how do I do that please I am new to Django and really not sure on how to do that

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [November 8, 2022, 12:50pm UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/12 "2022-11-08T12:50:46Z")

</div>

Here are the docs for:  
[serializers](https://www.django-rest-framework.org/api-guide/serializers/);  
[source attribute](https://www.django-rest-framework.org/api-guide/fields/#source);

The target serializer to inherit is: `rest_framework.authtoken.serializers.AuthTokenSerializer`

---

<div class="post-metadata">

**Author:** ![calcanisa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calcanisa/32/10066_2.png) [@calcanisa](https://forum.djangoproject.com/u/calcanisa)\
**Post date:** [November 8, 2022, 12:52pm UTC](https://forum.djangoproject.com/t/why-is-username-required-when-i-set-email-as-username/16886/13 "2022-11-08T12:52:21Z")

</div>

Ok thank you I will have a look
