Django 2.1 and Python 3 User Authentication System — #3(Using the AbstractBaseUser Class)

Abedy Ng'ang'a
2 min readJun 29, 2020

Before we begin, it is important that you read my previous article on the default Django User model as it contains all the necessary configurations to our project as well as a detailed explanation Django’s user model. If you’re new to Django on the other hand, you might want to read this first. With that, let’s get started.

As with the previous article, we’re not exactly going to be doing a lot. As a matter of fact we’re only going to be changing our models.py file in our users app, as well as adding a new file called managers.py in our users directory.

Let’s first get up to speed. In this approach, We’re going to define our own User model from scratch, without having to extend from the default User model as in this approach. To do that we’re going to need to define our own manager to handle things, hence the managers.py file.

In this example, we’re going to do away with the username field and use the email address for authentication. So let’s get started.

We’re first going to define our manager as follows:

Then we’re going to import that into our models and use it in our User class as follows:

One thing I forgot to mention, remember to include the following important configuration in your settings.py file:

AUTH_USER_MODEL = “users.User” # Logic: <"appname.UserClass">

And that’s basically it. From there run the normal:

python manage.py makemigrations

and then:

python manage.py migrate

And with that we’re done. Just run your server and everything should work just fine. Hope you enjoyed the read.

--

--