Django rest framework how to change: “This field may not be blank.” error message

When I am trying to create a new user model, I am getting the following validation error response:

HTTP 400 Bad Request
Allow: POST, OPTIONS
Content-Type: application/JSON
Vary: Accept

{
    "phone": [
        "This field may not be blank."
    ],
    "name": [
        "This field may not be blank."
    ]
}

What I want to do is how to change the error message to a custom one. Here is my user serializer code:

from rest_framework import serializers
from django.contrib.auth import get_user_model


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = get_user_model()
        fields = '__all__'
        read_only_fields = ('last_login',)

I already override the validate method but that didn’t work neither using this solution:

extra_kwargs = {"phone": {"error_messages": {"required": "A valid phone number is required"}}}

views.py:

class UserList(generics.CreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

(Custom user) User.py:

class User(AbstractBaseUser):
    """
    Custom user model based on phone number as the only identifier
    """
    phone = models.CharField(max_length=10, unique=True)
    name = models.CharField(max_length=50, blank=False)
    password = None

    USERNAME_FIELD = 'phone'

    objects = UserManager()

    def __str__(self):
        return self.name

User manager.py:

class UserManager(BaseUserManager):
    """
    Custom user model manager where the phone is the unique identifier
    """

    def create_user(self, phone, name, **extra_fields):
        """
        Create and save a User using the given phone number and country
        """
        if not phone:
            raise ValueError(_('A valid phone must be provided.'))
        if not name:
            raise ValueError(_('Your name is required'))
        user = self.model(phone=phone, name=name, **extra_fields)
        user.set_unusable_password()
        user.save(using=self._db)

    def create_superuser(self, phone, name, **extra_fields):
        """
        Create and save a SuperUser with the given phone and country.
        """
        extra_fields.setdefault('is_verified', True)
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))
        if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
        return self.create_user(phone, name, **extra_fields)

I know it’s a silly problem but I couldn’t solve it. Thanks for your appreciated help.

Answer

blank is the key for the error messages dict that is applicable for blank fields so:

extra_kwargs = {'phone': {'error_messages': {'blank': 'New blank error message'}}}