For custome User()
custom app → authentication
settings.py
AUTH_USER_MODEL = 'authentication.User'
authentication/models.py
class User(AbstractBaseUser,PermissionsMixin):
hero_name = models.CharField(max_length=255,unique=True, db_index=True)
first_name = models.CharField(max_length=255,null=True,blank=True)
last_name = models.CharField(max_length=255, null = True, blank=True)
phone_number = models.CharField(max_length=12,unique=True,null = True, blank=True)
zip_code = models.CharField(max_length=8,null=True,blank=True)
referal_code = models.CharField(max_length = 50, null = True, blank=True)
email = models.EmailField(max_length=255,unique=True, db_index=True,)
is_phone_verified = models.BooleanField(default=False)
is_email_verified = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True) #same as created_at Automatically set the field to now when the object is first created
updated_at = models.DateTimeField(auto_now=True) #Useful for “last-modified” timestamps.auto_now=True
role = models.CharField(max_length=255, null = True, blank=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['hero_name',]
objects = UserManager()
def __str__(self):
return self.email #access the class variable inside the class using self.<class variable>
def tokens(self):
'''return the user tokens'''
refresh = RefreshToken.for_user(self)
return {
'refresh' : str(refresh),
'access' : str(refresh.access_token),
}