Token authentication

139 views
Skip to first unread message

Ashutosh Mishra

unread,
Nov 7, 2020, 7:57:37 AM11/7/20
to Django REST framework
Hello guys I have a task.I have an authentication model in which there is a custom user model and nurse model,email and password will store in custom user model rest details of the nurse will store in the nurse model,and token will be generated.I am unable to figure out how can i do this,i am a newbie.Do help me.
i am sharing my models.py

class Account(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
)
type_choice=(('Nurse','Nurse'),
('Patient','Patient')
)
account_type = models.CharField(choices=type_choice, max_length=20, null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()

EMAIL_FIELD = 'email'
REQUIRED_FIELDS = ['email']

def __str__(self):
if self.account_type and self.email:
return str(self.email+"---->"+self.account_type)
# elif self.mobile:
# return str(self.mobile)
else:
return None


def has_perm(self, perm, obj=None):
return True

def has_module_perms(self, app_label):
return True

@property
def is_staff(self):
return self.is_admin

class Nurse(models.Model):
account=models.ForeignKey(Account,on_delete=models.CASCADE,max_length=50,blank=True,null=True)
name = models.CharField(max_length=255, null=False)
nurseid = models.CharField(max_length=255, null=False)
hospital = models.CharField(max_length=255, null=False)
speciality = models.CharField(max_length=255, null=False)
country = models.CharField(max_length=255, null=False)
city = models.CharField(max_length=255, null=False)
accesscode = models.CharField(max_length=255, null=False)
mobile = models.CharField(max_length=255, null=False)
created_date = models.DateTimeField(auto_now=True)
modified_date = models.DateTimeField(auto_now=True)

def __str__(self):
return str(self.account)

Sai Patibandla

unread,
Dec 1, 2020, 2:02:25 PM12/1/20
to Django REST framework
You can use django signals where you define a receiver function that listens to post_save signal from your user model. For the actual token creation, you can use Token module from rest_framework.authtoken.models with passing the user instance as parameter. 

from django.dispatch import receiver
from rest_framework.authtoken.models import Token

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(senderinstance=None, created=False, **kwards):
    if created:
        Token.objects.create(user=instance)

Praveen chaduvala

unread,
Jan 12, 2021, 7:15:16 AM1/12/21
to Django REST framework
This is best for generating tokens
add inside models
def tokens(self):
        refresh=RefreshToken.for_user(self)
       return {
       'refresh':str( ),
        'access':str(refresh.access_token)
        }
setting add 
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}

and after login return your token  
Reply all
Reply to author
Forward
0 new messages