Last active
February 7, 2016 18:20
-
-
Save makerj/f8ea102fd2fd9ce0a246 to your computer and use it in GitHub Desktop.
custom django user model
This file contains 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
class UserManager(BaseUserManager): | |
def create_user(self, username, password=None): | |
""" | |
Creates and saves a User with the given username and password | |
""" | |
if not username: | |
raise ValueError('Users must have an email address') | |
user = self.model( | |
username=username, | |
) | |
user.set_password(password) | |
user.save(using=self._db) | |
return user | |
def create_superuser(self, username, password=None): | |
""" | |
Creates and saves a User with the given username and password | |
""" | |
user = self.create_user(username=username, | |
password=password, | |
) | |
user.is_superuser = True | |
user.save(using=self._db) | |
return user | |
class User(AbstractBaseUser): | |
# mandatory ('password' is automatically created by django) | |
username = models.CharField(max_length=129, unique=True, db_index=True) | |
# properties | |
name = models.CharField(max_length=60, blank=True) | |
email = models.CharField(max_length=254, blank=True) | |
birth_date = models.DateField(null=True, blank=True) | |
profile_image = s3_field(blank=True) | |
profile_background_image = s3_field(blank=True) | |
profile_message = models.CharField(max_length=100, blank=True) | |
couple = models.ForeignKey(Couple, blank=True, null=True, db_index=True) | |
# metadata ('last_login' is automatically created by django) | |
created = models.DateTimeField(auto_now_add=True) | |
is_active = models.BooleanField(default=True) | |
is_staff = models.BooleanField(default=False) | |
is_superuser = models.BooleanField(default=False) | |
social = JSONField(blank=True, null=True) | |
# resolve django framework requirements | |
objects = UserManager() | |
USERNAME_FIELD = 'username' | |
REQUIRED_FIELDS = [] | |
def get_full_name(self): | |
return self.username | |
def get_short_name(self): | |
return self.username | |
def __str__(self): | |
return self.username | |
@property | |
def is_staff(self): | |
return self.is_staff or self.is_superuser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment