我是相当新的Django,想问关于用户身份验证和登录英寸我阅读文档,我无法找到正确的答案/方向关于我在尝试去完成。基本上,我必须整合到一个遗留系统中,所以我不能使用默认的auth_user
表或类似的东西。我在我的应用程序定义的模型下面写:用户认证Django与自定义用户模型和表
class User(models.Model):
class Meta:
db_table = 'user'
def __unicode__(self):
return self.first_name + ' ' + self.last_name
first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64)
email = models.CharField(max_length=64)
password = models.CharField(max_length=32)
active = models.CharField(max_length=1)
last_modified = models.DateTimeField("last modified")
timestamp = models.DateTimeField("timestamp")
我的问题是,我怎么能利用上述模式(或者我应该做哪些改变吧)与身份验证的应用程序工作?现在我有认证以下后端按文档:
class CustomAuth(ModelBackend):
def authenticate(**credentials):
return super(CustomAuth, self).authenticate(**credentials)
def authenticate(self, username=None, password=None):
# Check the username/password and return a User.
if username != None and password != None:
# Get the user
try:
user = User.objects.get(email=username)
if user.check_password(password):
logger.info('User is authenticated, logging user in')
return user
except User.DoesNotExist:
pass
return None
def get_user(self, user_id):
try:
return User.objects.get(id=user_id)
except User.DoesNotExist:
return None
我试图测试按如下:
user = authenticate(username='[email protected]', password='testUser')
我不断收到各种错误,如'module' object is not callable
。我还在settings.py
文件中包含了我的自定义身份验证。有没有我想要做的例子?任何帮助表示赞赏,谢谢!
编辑 我改变了我的模型,下面:
from django.db import models
from django.contrib.auth.models import User as AuthUser, UserManager
# Extend the base User model
class User(AuthUser):
class Meta:
db_table = 'user'
active = models.CharField(max_length=1)
last_modified = models.DateTimeField("last modified")
objects = UserManager()
你读过[this](http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/)和[this](http:// scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/)和[this](http://stackoverflow.com/questions/44109/extending-the-user- model-with-custom-fields-in-django),基本上你扩展/关联'默认'用户模型,而不是自己创建。 –
你说的是从构建到框架中的'User'模型继承?为什么我不想这样做,它需要我有不同的列,我不需要/希望,因为我正在集成到遗留数据模型中......或者我的理解是不正确的? – KVISH
我发现了一篇适合我的文章......不确定这是推荐的方式吗? http://tomforb.es/using-a-custom-sqlalchemy-users-model-with-django – KVISH