Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django custom login using own model

I am using Django1.4 with PostgreSQL. I am developing an application in which I have two models i.e. Students, Company.

class students(models.Model):

    first_name = models.CharField(**option)
    last_name = models.CharField(**option)
    username = models.EmailField(max_length=100, unique=True)
    password = models.CharField(_('password'), max_length=128)
    # Some other attributes for Student models



class company(models.Model):
    compnay_name = models.CharField(**option)
    username = models.EmailField(max_length=100, unique=True)
    password = models.CharField(_('password'), max_length=128)
    #Some other attributes for company models

My Requirement:

  1. Student and Company can create a new profile (provide a sign-up form)
  2. Which creating a new profile for Student/Company, username i.e. email id should be unique. i.e. Email id should not exist in Student & Company models.(task completed )
  3. Created 2 sign-In form for Student & Company login.

Issue:

  1. As I am not using or extending User model, I am cannot use django in-built login & authenticate method.

  2. How can I write a custom authentication method which should check user credentials in Student/Company username & password. (Have 2 different Sign-in form for Student & Company)

Please help me.

Thanks for reading my query.

backend.py

class LoginBackend:
    def authenticate(self, username=None, password=None, model=None):
        if model == "Student":
            lookup_model = Student
        elif model == "Employer":
            lookup_model = Employer
        try:
            user = lookup_model.objects.get(email=username)
         except Exception, e:
            return None
    return user

views.py

def check_auth(request):
    user_object = Student.objects.get(email__iexact = unicode(email))
    if check_password(password, user_object.password):
        print authenticate(username = email, password = password, model = "Student")
        login(request, user_object)

settings.py

AUTHENTICATION_BACKENDS = ("proj.app.backends.LoginBackend",)

Error

AttributeError at /xxx/login/

'Student' object has no attribute 'backend'

like image 228
jayapal d Avatar asked Aug 06 '12 05:08

jayapal d


1 Answers

Write a custom authentication backend. Read this:

  • Writing an authentication backend
  • Handling authorization in custom backends
  • settings.AUTHENTICATION_BACKENDS

[update]

By writing and registering a custom authentication backend, you just have to use the standard Django authentication patterns. Looking at your sample code, I'm under the impression that you have understood it differently.

Since email is your unique key, I suggest using email for the login key, first check the login/password against Student, and if it fails, check against Company.

from django.contrib.auth.models import User
class JayapalsBackend(object):
    def authenticate(self, username=None, password=None):
         try:
             o = Student.objects.get(email=username, password=password)
         except Student.DoesNotExist:
             try:
                 o = Company.objects.get(email=username, password=password)
             except Company.DoesNotExist:
                 return None
         return User.objects.get(email=o.email)
    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

Then just use standard Django decorators:

@login_required
def some_private_view(request):
    ...
like image 69
Paulo Scardine Avatar answered Oct 10 '22 23:10

Paulo Scardine