Creating custom user model and custom authentication in Django

creating custom user model in django

While working on some django project you might feel that the default user model is not fulfilling all the requirements. For example you may want to authenticate users by email Id and not by username. You may want to store some more extra information in the user model. In short you might want to define your own custom user model.

In particular we may encounter one out of below four scenarios:

  • You are happy with the way default authentication but you need to store extra information in the model itself.
  • You want different authentication process, for example using email, but do not want to store extra information.

We will discuss second approach here. Our approach will use custom defined user model and custom define backend authentication mechanism.

How to create a custom user model:
  1. First of all create the user model class in your models directory. Add the usermanager class also.
     

    Since I don’t require anything except email, password, is_active and  is_staff, I removed everything else from the model. Is_staff is required for user login in admin panel. If you are not going to use admin site then is_staff can be removed as well.

  2. Define this newly created custom user model in settings file. Project should know that we are going to use other than default user model.

  3.  As shown in code above, mention the name of custom authentication backend we will create to support our custom user model. AUTHENTICATION_BACKENDS  is list of backends. If first one fails to authenticate, second is used as fallback. Here first authentication backend in the list is the one we will create and second is the django’s default authentication backend.
  4. Create the custom authentication backend. backends.py .

     

  5. Now run makemigrations and migrations command. This will create the custom user model in database. (Assuming database settings are ok).
    $ python manage.py makemigrations $ python manage.py migrate
  6. Now create a super user. is_staff should be true for this user.
    $ python manage.py createsuperuser Running this command will ask you email id, is_staff, password and confirm password.
  7. After user is created successfully, go to admin login and confirm if you are able to login.
  8. If you are getting error  Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive , make sure you have is_staff field in model and has_perm and pas_module_perm methods define and they return true.
  9. If you are able to login successfully, Good.
How to Register Custom user model with Admin Site:
  1. In the above section, we created custom user model with them help of AbstractBaseUser . Custom user models created by this method are not registered normally with admin site. For this we need to override django’s default UserAdmin .
  2. In the app directory, create a file admin.py  if file do not exists already.

    Adequate comments are used in above code to make is easy to understand.

  3. Now restart the server and login to admin site. You will be able to see the users model and you will be able to add, edit and delete the model objects.

 

Let me know in case of any question.

 

References: https://docs.djangoproject.com/en/1.10/topics/auth/customizing/

(Visited 2,784 times, 1 visits today)

You must read this :

Leave a Reply

Your email address will not be published. Required fields are marked *