User authentication in django with custom user model and table -
i'm new django , wanted ask regarding user authentication , logging in. read documentation , unable find proper answer/direction regarding trying accomplish. basically, have integrate legacy system, cannot use default auth_user
tables or that. have model defined in app 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 make use of above model (or changes should making it) work authentication app? right have following backend authentication per docs:
class customauth(modelbackend): def authenticate(**credentials): return super(customauth, self).authenticate(**credentials) def authenticate(self, username=none, password=none): # check username/password , return user. if username != none , password != none: # user try: user = user.objects.get(email=username) if user.check_password(password): logger.info('user 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 test per below:
user = authenticate(username='test@gmail.com', password='testuser')
i keep getting various errors such 'module' object not callable
. included custom authentication in settings.py
file. there example of trying do? appreciated, thanks!
edit changed model below:
from django.db import models django.contrib.auth.models import user authuser, usermanager # extend 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()
it looks 1 problem custom backend has 2 authenticate
methods. want remove top one.
here's an example of how write custom backend processing email address based logins.
Comments
Post a Comment