I have a django application with specific fields, so i decided to create a new user model. I was following the documentation thoroughly, but i could not wrap my head around extending AbstractBaseUser and BaseUserManager from here.
1) Why would i need to extend two classes to create a single user model. I want to know if the fields we specify in MyUserManager and MyUser anyway similar.
2) Is there any similarities between that email field specified here in MyUserManager and MyUser or both are same?
In my app i want to use mobile number(Charfield max_length=12) as login neither email nor username
3) Django provided how to create a custom user model with email
then i have another specific question do i need to specify mobile field in create_user and create_superuser and AbstractBaseUser model as well.
If you provide an excerpt with mobile number as USERNAME_FIELD that would be very helpful.
I know these are basic django questions but if you want to create a custom user model you might come across these questions several times
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With