概述
作为迁移的一部分,我将修复一些数据建模缺陷并将其移植到Postgresql.
我真的很想使用django.contrib.auth,但我不清楚我需要做什么.我已经阅读了文档,并且知道我可以将所需的用户信息和我所拥有的“额外”信息分开并将其放入UserProfile中.
以前有人处理过吗?你采取了什么方法?
这是我的代码:
from django.db.models import get_model from django.contrib.auth.models import User from hashlib import sha1 class MyUserAuthBackend(object): def check_legacy_password(self,db_password,supplied_password): return constant_time_compare(sha1(supplied_password).hexdigest(),db_password) def authenticate(self,username=None,password=None): """ Authenticate a user based on email address as the user name. """ try: user = User.objects.get(email=username) if '$' not in user.password: if self.check_legacy_password(user.password,password): user.set_password(password) user.save() return user else: return None else: if user.check_password(password): return user except User.DoesNotExist: return None def get_user(self,user_id): """ Get a User object from the user_id. """ try: return User.objects.get(pk=user_id) except User.DoesNotExist: return None
AUTHENTICATION_BACKENDS = ( 'my_website.my_app.my_file.MyUserAuthBackend',)
来自@Dougal的建议似乎是Django的下一个版本,并且不适用于我(我使用的是1.3.1).但是,它似乎是一个更好的解决方案.
总结
以上是编程之家为你收集整理的python – 将密码字段迁移到Django全部内容,希望文章能够帮你解决python – 将密码字段迁移到Django所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧