在Django的自定义用户自定义身份验证? [英] Custom Authentication with Custom User in django?
问题描述
我知道有一个文档,这个问题在这里 https://开头的文档。 djangoproject.com/en/1.9/topics/auth/customizing/ ,但是我需要澄清一些对我的用例。
I know there is documentation for this question here https://docs.djangoproject.com/en/1.9/topics/auth/customizing/, however I need some clarification on my use case.
我基本上我创建的API。这是什么API确实是用户提交表单数据到外部URL,而一旦提交,将数据保存到外部URL数据库。
I basically have an api that I created. What this api does is submit user form data to an external url, and once submitted, the data is saved to that external urls database.
然后我就可以打通另一个API调用特定的用户。用户返回作为对象。现在我试图登录该用户在我结束使用django.contrib.auth通过我的系统,但我有什么不工作。
I can then get a specific user through another api call. The user is returned as an object. Now I am trying to log that user in on my end using the django.contrib.auth through my system but what I have isn't working.
下面是我的过程:
一个。我添加了一个自定义的认证后端到我的settings.py文件
A. I added a custom authentication backend to my settings.py file
AUTHENTICATION_BACKENDS =(
'new_app.backends.SettingsBackend',
'django.contrib.auth.backends.ModelBackend',
)
乙。在我NEW_APP的后端文件,我有以下的code:
B. In my new_app's backend file, I have the following code:
class SettingsBackend(object):
def authenticate(self, username=None, password=None):
if username and password:
user = api.get_user()
#add pk to user object to satisfy django error warning
user.pk = user.unique_field
return user
return None
def get_user(self, user_id):
try:
if self.user.pk == user_id:
return self.user
except:
return None
在一个登入查看,我试图记录用户使用这样的:
In a signin view, I am trying to log the user in using this:
def login_user(request, username, password):
from django.contrib import auth
if not username and password:
raise ValueError("Invalid username and password")
auth_user = auth.authenticate(username=username, password=password)
if auth_user is None:
raise ValueError("Invalid username and password")
auth.login(request, auth_user)
return auth_user
我得到的错误说:
I get the error saying:
'api.get_user()' object has no attribute 'save'
该错误是发生在 auth.login(请求,AUTH_USER)
行。
我想原因是因为django.contrib.auth认为我的用户对象是一个Django模型。因此,问题是我应该如何解决这个做给我的情况下工作?感谢您的时间。
I think the reason is because the django.contrib.auth thinks my user object is a django model. So, the question is how should I tackle this to make it work given my scenario? Thanks for the time.
推荐答案
我不是在Django的细微之处的专家(因为它已经有一段时间我离开microframeworks的Django)。
I'm not an expert at django subtleties (as it's been a while I left django for microframeworks).
这是说,如果我是你,我会做的就是确保,无论 api.get_user()
正在恢复,实施任何API Django是从期待该对象,这意味着开始与一个空保存()
方法。
That said, if I were you, what I'd do is to make sure that whatever api.get_user()
is returning, implement whatever API django is expecting from that object, which means starting with an empty save()
method on it.
但我也愿意做标准的Django的东西,如@ RA123暗示的评论,通过设置 AUTH_USER_MODEL
了。
But I'd also do standard django stuff, like @RA123 suggests in a comment, by setting AUTH_USER_MODEL
up.
这篇关于在Django的自定义用户自定义身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!