Django come with good and enough authentication for most of cases, which is why I love using it. . But may be some time its not fulfill you need. Django default authentication that come with django use username and password to login in django admin panel or for authentication.
But am working on a project in with default authentication is not enough for login. Because i need to use email instead of username to login or authenticate a user.
Django has already a good documentation, in which they explained very well each and everything. But here am going to make it more simpler to change default authentication. Mean am going to use email id for login, in the place of username.
So here are the process that you need to follow to make it working:
- Create a new separate app, lets call it authentication. To create a app you can use django command.
django-admin.py startapp authentication
It will create a new folder, authentication into your project
- In the models.py file in your app authentication, add a custom user manager that deals with creating a user while treating the email as the unique identifier, instead of the username like so:
from django.contrib.auth.models import BaseUserManager class UserManager(BaseUserManager): def create_user(self, email, first_name, last_name, password=None): """ Creates and saves a User with the given email and password. """ if not email: raise ValueError('Users must have an email address') user = self.model( email=self.normalize_email(email), first_name=first_name, last_name=last_name) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, first_name, last_name, password): """ Creates and saves a superuser with the given email and password. """ user = self.create_user(email, first_name, last_name, password) user.is_staff = True user.is_superuser = True user.save(using=self._db) return user
- This custom UserManger is required, because we are going using custom user model instead of default one.
- Create a custom user model in same file as above.
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin from django.utils.translation import ugettext_lazy as _ class User(AbstractBaseUser, PermissionsMixin): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) email = models.EmailField(verbose_name=_('email address'), unique=True) is_staff = models.BooleanField(default=False) is_active = models.BooleanField(default=True) date_joined = models.DateTimeField(auto_now=True) REQUIRED_FIELDS = ['first_name', 'last_name'] USERNAME_FIELD = 'email' objects = UserManager() def __str__(self): return self.email def get_full_name(self): return '%s %s' % (self.first_name, self.last_name) def get_short_name(self): return self.email
- This is essentially the same as the default User model for Django, except “USERNAME_FIELD” is set to ‘email’ and some of the fields are removed.
- Django allows you to override the default user model by providing a value for the AUTH_USER_MODEL setting that references a custom model. Then finally in your settings.py file, you need to add custom user model like
AUTH_USER_MODEL = 'authentication.User'
- After this, you should be able to run makemigrations then migrate and authentication should work fine without requiring a username.
python manage.py makemigrations python manage.py migrate
- To test create a superuser, and email, first_name, last_name and password will be prompted.
python manage.py createsuperuser
Hopefully that went smoothly for you. You should check out the official docs for more information