title: Django custom login using own model
body: <p>I am using Django1.4 with PostgreSQL. I am developing an application in which I have two models i.e. Students, Company. </p>
  
  <pre><code>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
  </code></pre>
  
  <h3>My Requirement:</h3>
  
  <ol>
  <li>Student and Company can create a new profile (provide a sign-up form)</li>
  <li>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 &amp; Company models.(task completed )</li>
  <li>Created 2 sign-In form for Student &amp; Company login.</li>
  </ol>
  
  <h3>Issue:</h3>
  
  <ol>
  <li><p>As I am not using or extending User model, I am cannot use django in-built login &amp; authenticate method.</p></li>
  <li><p>How can I write a custom authentication method which should check user credentials in Student/Company username &amp; password. (Have 2 different Sign-in form for Student &amp; Company)</p></li>
  </ol>
  
  <p>Please help me.</p>
  
  <p>Thanks for reading my query. </p>
  
  <h3>backend.py</h3>
  
  <pre><code>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
  </code></pre>
  
  <h2>views.py</h2>
  
  <pre><code>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)
  </code></pre>
  
  <h2>settings.py</h2>
  
  <p>AUTHENTICATION_BACKENDS = ("proj.app.backends.LoginBackend",)</p>
  
  <h2>Error</h2>
  
  <p><strong>AttributeError at /xxx/login/</strong></p>
  
  <p><strong>'Student' object has no attribute 'backend'</strong></p>