我可以将自定义模型与 Django 的身份验证系统一起使用吗?
我为我的应用程序编写了一个自定义后端,以一种独特的方式处理登录,因为我对此项目有特定的需求。这是我的后端:
from my.project.models import User
from hashlib import sha512
class MyBackend:
def authenticate(self, email_address=None, password=None):
print "Trying to auth: " + email_address
try:
user = User.objects.get(email_address=email_address)
password = sha512(password + user.password_salt).hexdigest()
if user.password != password:
return None
else:
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
这是我的 User
类:
class User(models.Model):
email_address = models.EmailAddressField()
password = models.CharField(max_length=128)
password_salt = models.CharField(max_length=128)
它非常简单,但我已经围绕这个“User”类构建了其余的模型。
有没有办法让这项工作达到两全其美的效果,或者我应该放弃这种方法而只使用 Django 的内置模型供用户使用?
I wrote a custom backend for my application to process logins in a sort-of unique way, as I have specific needs for this project. Here's my backend:
from my.project.models import User
from hashlib import sha512
class MyBackend:
def authenticate(self, email_address=None, password=None):
print "Trying to auth: " + email_address
try:
user = User.objects.get(email_address=email_address)
password = sha512(password + user.password_salt).hexdigest()
if user.password != password:
return None
else:
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Here's my User
class:
class User(models.Model):
email_address = models.EmailAddressField()
password = models.CharField(max_length=128)
password_salt = models.CharField(max_length=128)
It's pretty simple, but I've already built the rest of my models around this 'User' class.
Is there a way to make this work so as to have the best of both worlds, or should I ditch this approach and just use Django's built-in model for users?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好通过使用用户配置文件。
定义用户配置文件模型,并在底部添加类似的内容:
我认为这种方法可能比更换用户模型更好
Extending the user can best be done by using user profiles.
Define a user profile-model and at the bottom, add something like this:
I think this approach might be better than replacing the User-model