如何登录到Django的休息可浏览API时,我有一个自定义的身份验证模式?(How do I log

2019-09-26 03:31发布

我有一个自定义的用户模型为如下account/models.py

from django.contrib.auth.modles import AbstractUser
from django.db.models.signals import post_save
from rest_framework.authtoken.models import Token
from django.db import models
from django.dispatch import receiver
from django.conf import settings

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

class UserProfile(AbstractUser):                                                     
    gender = models.CharField(max_length=1,default='') 

settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    )
}

...

AUTH_USER_MODEL = "account.UserProfile"

然而,每当我试图登录到浏览的API,它要求我使用正确的用户名和密码,我使用谁都是标记为超级用户和员工用户的凭据。

manage.py runserver控制台显示这条消息:

[27/Jul/2016 20:41:39] "POST /api-auth/login/ HTTP/1.1" 200 2897

Answer 1:

我记得那是因为内置的DRF AUTH形式不使用TokenAuthentication,而是SessionAuthentication我碰到在此之前也和。 尝试添加rest_framework.authentication.SessionAuthenticationDEFAULT_AUTHENTICATION_CLASSES元组



文章来源: How do I login to the Django Rest browsable API when I have a custom auth model?