gpt4 book ai didi

python - 当我有自定义身份验证模型时,如何登录 Django Rest 可浏览 API?

转载 作者:行者123 更新时间:2023-11-30 22:52:20 25 4
gpt4 key购买 nike

我有一个自定义用户模型,如下 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 时,它都会要求我使用正确的用户名和密码,并且我使用的用户凭据均被标记为 super 用户和员工。

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

[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 元组

关于python - 当我有自定义身份验证模型时,如何登录 Django Rest 可浏览 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38623002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com