python - Set email as username in Django 1.5 -
i reading docs at: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#substituting-a-custom-user-model
so in settings.py put:
auth_user_model = 'membership.user'
and in membership app models.py have this:
from django.contrib.auth.models import abstractbaseuser class user(abstractbaseuser): username_field = 'email'
running python manage.py syncdb giving me:
fielddoesnotexist: user has no field named 'email'
i check abstractbaseuser class source , field defined of course, can see here: https://github.com/django/django/blob/master/django/contrib/auth/models.py#l359
what's wrong?
abstractbaseuser
doesn't have email field, abstractuser
does.
if want use email unique identifier, need subclass abstractbaseuser , define email field unique=true
, write other functionality, example manager
of model:
from django.contrib.auth.models import abstractbaseuser, baseusermanager,\ permissionsmixin django.db import models django.utils.translation import ugettext_lazy _ django.utils import timezone django.utils.http import urlquote class customusermanager(baseusermanager): def create_user(self, email, password=none, **extra_fields): """ creates , saves user given email , password. """ = timezone.now() if not email: raise valueerror('the given email must set') email = customusermanager.normalize_email(email) user = self.model(email=email, is_staff=false, is_active=true, is_superuser=false, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, password, **extra_fields): u = self.create_user(email, password, **extra_fields) u.is_staff = true u.is_active = true u.is_superuser = true u.save(using=self._db) return u class user(abstractbaseuser, permissionsmixin): email = models.emailfield(_('email address'), unique=true) first_name = models.charfield(_('first name'), max_length=30, blank=true) last_name = models.charfield(_('last name'), max_length=30, blank=true) is_staff = models.booleanfield(_('staff status'), default=false, help_text=_('designates whether user can log admin ' 'site.')) is_active = models.booleanfield(_('active'), default=true, help_text=_('designates whether user should treated ' 'active. unselect instead of deleting accounts.')) date_joined = models.datetimefield(_('date joined'), default=timezone.now) objects = customusermanager() username_field = 'email' class meta: verbose_name = _('user') verbose_name_plural = _('users') def get_absolute_url(self): return "/users/%s/" % urlquote(self.pk) def get_full_name(self): """ returns first_name plus last_name, space in between. """ full_name = '%s %s' % (self.first_name, self.last_name) return full_name.strip() def get_short_name(self): "returns short name user." return self.first_name # define here other needed methods # @ django.contrib.auth.models.abstractuser
also, you'll want add user admin page. @ useradmin , redefine compatible new user model, use email field unique identifier.
Comments
Post a Comment