Skip to content

Instantly share code, notes, and snippets.

@johanmeiring
Created September 16, 2013 14:22
Show Gist options
  • Save johanmeiring/6581349 to your computer and use it in GitHub Desktop.
Save johanmeiring/6581349 to your computer and use it in GitHub Desktop.
Basic Custom User Model for Django 1.5
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin
class UserManager(BaseUserManager):
def create_user(self, email, first_name, surname, password=None):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=UserManager.normalize_email(email),
first_name=first_name,
surname=surname,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, first_name, surname, password):
user = self.create_user(
email,
first_name=first_name,
surname=surname,
password=password,
)
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
max_length=255,
unique=True,
db_index=True,
)
first_name = models.CharField(max_length=100)
surname = models.CharField(max_length=100)
is_staff = models.BooleanField(default=False) # Allows admin site access.
is_active = models.BooleanField(default=True, db_index=True)
date_joined = models.DateTimeField(default=timezone.now, db_index=True)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'surname']
def __unicode__(self):
return self.email
@property
def get_full_name(self):
return self.full_name
@property
def get_short_name(self):
return self.first_name.strip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment