gpt4 book ai didi

使用自定义模型用户进行 Django 身份验证

转载 作者:行者123 更新时间:2023-12-04 05:49:03 26 4
gpt4 key购买 nike

对于我的需要,内置模型 User 是不够的......所以我有我自己的模型 UserProfile 并且我想通过这个模型在现场进行身份验证(UserProfile 不是从 User 模型继承的,也根本不相关)。

我的型号:

class UserProfile(models.Model):
password = models.CharField(max_length = 40)
email = models.EmailField(max_length = 72, unique = True)

## Add this so that you can use request.user.is_authenticated
def is_authenticated(self):
return True

但是内置身份验证使用模型用户。

所以我想了解如何更改它,因此身份验证使用我的模型 UserProfile 和所有身份验证功能? ?

一个好的教程会很棒!

(逐步介绍 View 、模型和身份验证)

PS:我知道我可以在其他模型中存储额外的数据,但我不希望那样

最佳答案

这是一个更极端的例子,但说明了你想做的事情是可以做到的。作者不仅替换了身份验证后端使用的 User 模型,还使用了 SQLAlchemy 代替了 Django ORM。 http://tomforb.es/using-a-custom-sqlalchemy-users-model-with-django

重点是你需要写你的后端authenticateget_user方法来检索您的自定义用户模型。如果你还想支持权限,你需要写 has_perm .

关于使用自定义模型用户进行 Django 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10316460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com