当我有自定义验证模型时,如何登录Django Rest可浏览的API? [英] How do I login to the Django Rest browsable API when I have a custom auth model?

查看:137
本文介绍了当我有自定义验证模型时,如何登录Django Rest可浏览的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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时,它会要求我使用正确的用户名和密码,我正在使用同时被标记为超级用户和员工的用户的凭据。

However, whenever I try to log into the browsable API, it asks me to use a correct username and password, and I am using credentials of users who are both marked as superusers and staff.

manage.py runserver 控制台显示此状态消息:

The manage.py runserver console shows this status message:

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


推荐答案

我遇到了之前,从我记得这是因为内置的DRF身份验证形式不使用TokenAuthentication,而是SessionAuthentication。尝试添加 rest_framework.authentication.SessionAuthentication 到您的 DEFAULT_AUTHENTICATION_CLASSES 元组

I've ran into this before too and from what I remember it's because the built-in DRF auth form is not using TokenAuthentication, but rather SessionAuthentication. Try adding rest_framework.authentication.SessionAuthentication to your DEFAULT_AUTHENTICATION_CLASSES tuple

这篇关于当我有自定义验证模型时,如何登录Django Rest可浏览的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆