Some tips for MetaMask Mobile Users

We are thrilled to announce that OpenBlox is ready to welcome new Bloxes (https://presale.openblox.io/). As whitelist participants asked about what the minting process would look like, we decided to…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Configure a Custom User Model in Django

Django is a very powerful web framework built for python developers with the aim of quickly building web applications using a concept called “Batteries included” where loads of features have been simplified within the framework.

In this tutorial, we are going to learn how to build a custom user model in Django. Why do we need to build a custom user model, what is wrong with the existing user model, you may ask. By default, Django’s user model requires a username and password to create a user. However, modern web applications would rather use email and password to create and verify users. This is because an email is unique to just one person and that email can be used to identify a particular user. A username method will result in either multiple users having the same username or users having undesired usernames. Another reason why the email method works better is that it also serves as a way to send messages to your users about updates on the application and other important information. Enough of the theory and let’s jump into some code.

This tutorial assumes you have virtualenv installed or some other way of creating a private environment for your project like docker and you are working from said environment.

Firstly, we would create our Django project called userproject and app called core using the following commands:

In this section, we are going to create some tests for our custom user model. This method of writing test code before the actual code is referred to as Test Driven Development (TDD). It helps us to think of our code before writing it, thereby helping us write better quality code. Before writing our tests, we would add our core app to the settings.py file so the app can be recognized by the project.

Next, we add the following code to our test.py file in the core app

In this section, we would be implementing the Custom User Model. We would be using classes such as AbstractBaseUser, BaseUserManager, and PermissionsMixin.

We can now run our migrations and tests again.

This time Django will successfully run the four tests that we have written for our Custom User model. We now have a User model that uses only email and password for authentication.

I hope this helps you. Thanks for reading.

Add a comment

Related posts:

Dear You

When I first seen you, I tried my hardest not to fall for you.. “Dear You” is published by Katherine M. Gamble in a Few Words.

Ancient Ruins V

The man who stepped forward called himself Zed. He was a tall man and towered over her. Slim and lean, he looked like he’d been half starved, but he was older and that might have just been his way…

Hammond Sportsplex Meets a Distinct Indiana Community Need

Based in Orland Park, Illinois, Rob Ferrino guides Madison Construction Company and directs coordinated developments across the Midwest. One major Indiana project that Rob Ferrino helped facilitate…