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(sender, instance=None, created=False, **kwards):
    if created:
        Token.objects.create(user=instance)

On Saturday, November 7, 2020 at 6:57:37 AM UTC-6 ashutosh...@gmail.com 
wrote:

> 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)
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/7804bef1-98f1-48a8-a86c-cf7449b8261dn%40googlegroups.com.

Reply via email to