Open In App

Customizing Phone Number Authentication in Django

Last Updated : 21 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We will implement phone number-based authentication in Django by creating a custom user model that uses phone numbers instead of usernames for login. You’ll learn how to build a custom user manager, update settings, and integrate the new user model with the Django admin panel to enhance security and streamline the authentication process.

Create Project and App

Prerequisites:

To start the project use this command

django-admin startproject newton
cd newton
python manage.py startapp accounts

File Structure:

File Structure

Note: Phone number authentication is a critical feature in modern web applications. To implement this and other advanced features, the Django Web Development Course will help you build secure and user-friendly systems.

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"accounts", //App name
]

Create the Custom User Model

In accounts/models.py, define a custom user model that uses phone instead of username:

Python
from django.db import models
from django.contrib.auth.models import AbstractUser
from .manager import UserManager

class GFG(AbstractUser):
    phone = models.CharField(max_length=12, unique=True)
    USERNAME_FIELD = 'phone'
    REQUIRED_FIELDS = []
    objects = UserManager()

Create a Custom User Manager

accounts/manager.py: We import BaseUserManager from django.contrib.auth.base_user to create a custom user manager.We define a custom UserManager class that extends BaseUserManager. This manager is responsible for creating and managing user instances.The create_user method is used to create a standard user with a phone number, an optional password, and any extra fields passed as keyword arguments. It checks if a phone number is provided and raises a ValueError if not.

Python
from django.contrib.auth.base_user import BaseUserManager
class UserManager(BaseUserManager):
    use_in_migrations = True
 
    def create(self, phone, password=None, **extra_fields):
        if not phone:
            raise ValueError('Phone number is required')
            user = self.model(phone=phone, **extra_fields)
        user.set_password(password)
        user.save()
        return user
    def superuser(self, phone, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)
        return self.create(phone, password, **extra_fields)

Register the Model with Admin

In accounts/admin.py:

Python
from django.contrib import admin
from .models import GFG

admin.site.register(GFG)

Update Django Settings

In newton/settings.py, tell Django to use your custom user model:

Python
AUTH_USER_MODEL = "accounts.GFG" #Appname.model(classname)

Run Migrations

Make and apply migrations for your custom user model:

python manage.py makemigrations
python manage.py migrate

Run the Server

Start the development server:

python manage.py runserver

Output

Django Administration



Next Article

Similar Reads