Trying to generate token of custom user but getting above error,

models.py
class MyUserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
raise ValueError('Users must have an email address')

account = self.model(
email=self.normalize_email(email),

)
account.account_type = extra_fields.get('account_type')
account.set_password(password)
account.save(using=self._db)
return account

def create_superuser(self, email, password, **extra_fields):
account = self.create_user(
email,
password=password,
)
account.account_type = 'A'
account.is_admin = True
account.save(using=self._db)
return account


class Account(AbstractBaseUser):
name=models.CharField(blank=True,null=True,max_length=255)
Address=models.CharField(blank=True,null=True,max_length=255)
type_choice = (
('A', 'Admin'),
('S','Student'),
('T','Teacher'),
)
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=False,
)
account_type = models.CharField(choices=type_choice, max_length=1, 
null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()

USERNAME_FIELD = 'email'

def __str__(self):
return self.email

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

VIEWS.PY
class AccountViewSet(viewsets.ViewSet):
def create(self,request):
permission_classes = [TokenHasReadWriteScope]
try:
name=request.data.get('name')
Address=request.data.get('Address')
account_type=request.data.get('account_type')
if not all([name,Address,account_type]):
raise Exception('All Fields are mandatory')

obj=Account()
obj.name=name
obj.Address=Address
obj.account_type=account_type
obj.save()
print('hello')
app = Application.objects.get(user=obj)
token = generate_token()
print('token',token)
refresh_token = generate_token()
expires = now() + 
timedelta(seconds=oauth2_settings.ACCESS_TOKEN_EXPIRE_SECONDS)
scope = "read write"
access_token = AccessToken.objects.create(user=obj,
application=app,
expires=expires,
token=token,
scope=scope,
)
print("access token ------->", access_token)
RefreshToken.objects.create(user=obj,
application=app,
token=refresh_token,
access_token=access_token
)
return Response({"response":'delivered'})
except Exception as error:
traceback.print_exc()
return Response({"message": str(error), "success": False}, 
status=status.HTTP_200_OK)

-- 
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/47c552e0-2ed0-4085-9866-a921dffb2eb9n%40googlegroups.com.

Reply via email to