Reputation: 13178
I'm fairly new to Django and wanted to ask regarding user authentication and logging in. I read the documentation and I was unable to find proper answer/direction regarding what I am trying to accomplish. Basically, I have to integrate into a legacy system, so I cannot use the default auth_user
tables or anything like that. I have a model defined in my app as written below:
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")
My question is, how can I make use of the above model (or what changes should I be making to it) to work with the authentication app? Right now I have the following backend for authentication as per the docs:
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
I tried to test as per below:
user = authenticate(username='[email protected]', password='testUser')
I keep getting various errors such as 'module' object is not callable
. I also included my custom authentication in the settings.py
file. Is there an example of what I am trying to do? Any help is appreciated, thanks!
EDIT I changed my model to the below:
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()
Upvotes: 1
Views: 10071
Reputation: 3040
It looks like one problem is that your custom backend has two authenticate
methods. You probably want to remove the top one.
Here's an example of how to write a custom backend for processing email address based logins.
Upvotes: 3