我可以将自定义模型与 Django 的身份验证系统一起使用吗?

发布于 2024-12-05 06:35:56 字数 1097 浏览 0 评论 0原文

我为我的应用程序编写了一个自定义后端,以一种独特的方式处理登录,因为我对此项目有特定的需求。这是我的后端:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2024-12-12 06:35:56

最好通过使用用户配置文件
定义用户配置文件模型,并在底部添加类似的内容:

User.profile = property(lambda u: Profile.objects.get_or_create(user=u)[0])

我认为这种方法可能比更换用户模型更好

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文