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.
它非常简单,但是我已经围绕这个“User”类构建了我的其余模型。
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?
是否有一种方法可以使这一功能发挥到极致,或者我应该抛弃这种方法,只对用户使用Django的内置模型?
1 个解决方案
#1
2
Extending the user can best be done by using user profiles. Define a user profile-model and at the bottom, add something like this:
扩展用户可以通过使用用户配置文件来完成。定义一个用户概要文件模型,在底部添加如下内容:
User.profile = property(lambda u: Profile.objects.get_or_create(user=u)[0])
I think this approach might be better than replacing the User-model
我认为这种方法可能比替换用户模型更好
#1
2
Extending the user can best be done by using user profiles. Define a user profile-model and at the bottom, add something like this:
扩展用户可以通过使用用户配置文件来完成。定义一个用户概要文件模型,在底部添加如下内容:
User.profile = property(lambda u: Profile.objects.get_or_create(user=u)[0])
I think this approach might be better than replacing the User-model
我认为这种方法可能比替换用户模型更好