[英]Django-Rest-Framework: How to post to foreignkey field using request.user for logged in user
[英]Authentication loop in django-rest-framework when accessing request.user
我在django-rest-framework 3.0.3中定义了一个自定义身份验证方案,如下所示:
from django.contrib.auth.models import AnonymousUser
from rest_framework import authentication
class CustomAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
print 'authenticate!', request.user
return (AnonymousUser(), None)
当我使用CustomAuthentication
,出现“超过最大递归深度”错误,并且此错误打印在我的日志中:
authenticate! authenticate! authenticate! authenticate! authenticate! etc.
从我的打印语句中删除request.user
可修复此无限循环。 定义自定义身份验证方案时,我不应该使用request.user
吗?
如本文件所示:
尽管该行为取决于所使用的身份验证策略,但request.user通常返回django.contrib.auth.models.User的实例。
如果请求未经身份验证,则request.user的默认值是django.contrib.auth.models.AnonymousUser的实例。
我认为使用request.user时,它将检查请求是否具有用户对象,并递归不断调用您的自定义身份验证方法。 您需要在函数中返回User对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.