Last Updated : 23 Jul, 2025
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 AppPrerequisites:
To start the project use this command
File Structure: File Structuredjango-admin startproject newton
cd newton
python manage.py startapp accounts
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.
Create the Custom User ModelINSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"accounts", //App name
]
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:
Run the Serverpython manage.py makemigrations
python manage.py migrate
Start the development server:
python manage.py runserver
Output
Django AdministrationRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4