[Django]-Django AbstractBaseUser vs BaseUserManager when creating custom user

8👍

Answer to question 1
You need to extend two classes because Django has two classes for users. One is the User model and the other is the UserManager. For simple tasks we don’t need to tinker with the manager class. But for your purpose you do need it. Please note that the manager class does NOT have any field names, rather it has two methods. One method is used to create regular users, the other method is used to create superusers. These methods are called when you use commands like python manage.py createsuperuser.

It is recommended in the Django docs that the best way to extend user classes is to just create another class for extra information. See this answer.

Answer to question 2
There is no email field in MyUserManager class, look carefully.

Answer to question 3
If I were you I would just save the mobile number in the username field. That way all the background tasks of mobile being the USERNAME_FIELD is already taken care of. However, if you want to extend your user class then I suppose you have to mention the mobile field in the manager class methods as they are used to create users.

Note:- Please look up validators and use it to validate your mobile numbers. This will check if the users who are providing mobile numbers have the correct format. I’m guessing your system will send a code to the user’s mobile as a text message through an SMS gateway API. You don’t want to waste money on text messages that doesn’t get received due to user errors.

Leave a comment