Created
December 5, 2017 11:07
-
-
Save bencleary/15d3f4d00701ebb4906fa971aa82ec40 to your computer and use it in GitHub Desktop.
An example of building a user model which uses email as username rather than username
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from django.contrib.auth.admin import UserAdmin | |
from django.contrib.auth.forms import UserCreationForm | |
from .models import * | |
class UserAdmin(UserAdmin): | |
add_form = BlogUserCreationForm | |
add_fieldsets = ( | |
(None, { | |
'classes': ('wide',), | |
'fields': ('email', 'first_name', 'last_name', 'username', 'password1', 'password2',), | |
}), | |
) | |
inlines = (BlogUserProfile, ) | |
admin.site.register(BlogUser, UserAdmin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import * | |
from django.core import validators | |
from django.utils.translation import ugettext_lazy as _ | |
from django.db import models | |
class BlogUser(AbstractBaseUser, PermissionsMixin): | |
email = models.EmailField( | |
_('Email Address'), unique=True, | |
error_messages={ | |
'unique': _("A user with that email already exists."), | |
} | |
) | |
username = models.CharField( | |
_('Username'), max_length=30, unique=True, blank=True, null=True, | |
help_text=_('30 characters or fewer. Letters, digits and _ only.'), | |
validators=[ | |
validators.RegexValidator( | |
r'^\w+$', | |
_('Enter a valid username. This value may contain only ' | |
'letters, numbers and _ character.'), | |
'invalid' | |
), | |
], | |
error_messages={ | |
'unique': _("The username is already taken."), | |
} | |
) | |
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 the user can log into this admin ' | |
'site.') | |
) | |
is_active = models.BooleanField( | |
_('Active'), default=True, | |
help_text=_('Designates whether this user should be treated as ' | |
'active. Unselect this instead of deleting accounts.') | |
) | |
### | |
# the next two fields are custom for my app for email validation | |
### | |
is_verified = models.BooleanField( | |
_('Verified'), default=False, | |
help_text=_('Designates whether this user has verified an email account') | |
) | |
verified = models.DateTimeField(_('Verified Date'), blank=True, null=True, help_text=_( | |
'Stored value of when email activation was completed' | |
)) | |
#### | |
date_joined = models.DateTimeField(_('Date Joined'), default=timezone.now) | |
objects = UserManager() | |
USERNAME_FIELD = 'email' | |
REQUIRED_FIELDS = ['username'] | |
class Meta(object): | |
verbose_name = _('User') | |
verbose_name_plural = _('Users') | |
abstract = False | |
def get_full_name(self): | |
""" | |
Returns email instead of the fullname for the user. | |
""" | |
return "{0} {1}".format(self.first_name, self.last_name) | |
def get_short_name(self): | |
""" | |
Returns the short name for the user. | |
This function works the same as `get_full_name` method. | |
It's just included for django built-in user comparability. | |
""" | |
return self.get_full_name() | |
def __str__(self): | |
return self.email | |
def email_user(self, subject, message, from_email=None, **kwargs): | |
""" | |
Sends an email to this User. | |
""" | |
send_mail(subject, message, from_email, [self.email], **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make sure you declare your custom model in settings | |
AUTH_USER_MODEL = 'users.BlogUser' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment