1

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='test@gmail.com', 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()
KVISH
  • 12,923
  • 17
  • 86
  • 162
  • have you read [this](http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/) and [this](http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/) and [this](http://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django), basically you extend/relate the 'default' user model rather then make your own. – T I Jul 01 '12 at 19:53
  • You're talking about inheriting from `User` model built into the framework? The reason why I didn't want to do it that way, it requires me to have various columns I don't need/want since i'm integrating into a legacy data model...or is my understanding incorrect? – KVISH Jul 01 '12 at 21:40
  • 1
    I found an article which works for me...not sure if this is the recommended way? http://tomforb.es/using-a-custom-sqlalchemy-users-model-with-django – KVISH Jul 01 '12 at 21:40

1 Answers1

3

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.

cberner
  • 3,000
  • 3
  • 22
  • 34