Last active
January 27, 2018 20:00
-
-
Save toransahu/4a6314f40676a75b0288953f5c0e8b1c to your computer and use it in GitHub Desktop.
Django | Extend User Model | Extending User Model Using a Custom Model Extending AbstractBaseUser
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
from __future__ import unicode_literals | |
from django.db import models | |
from django.core.mail import send_mail | |
from django.contrib.auth.models import PermissionsMixin | |
from django.contrib.auth.base_user import AbstractBaseUser | |
from django.utils.translation import ugettext_lazy as _ | |
from .managers import UserManager | |
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) | |
date_joined = models.DateTimeField(_('date joined'), auto_now_add=True) | |
is_active = models.BooleanField(_('active'), default=True) | |
avatar = models.ImageField(upload_to='avatars/', null=True, blank=True) | |
objects = UserManager() | |
USERNAME_FIELD = 'email' | |
REQUIRED_FIELDS = [] | |
class Meta: | |
verbose_name = _('user') | |
verbose_name_plural = _('users') | |
def get_full_name(self): | |
''' | |
Returns the first_name plus the last_name, with a space in between. | |
''' | |
full_name = '%s %s' % (self.first_name, self.last_name) | |
return full_name.strip() | |
def get_short_name(self): | |
''' | |
Returns the short name for the user. | |
''' | |
return self.first_name | |
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) | |
from django.contrib.auth.base_user import BaseUserManager | |
class UserManager(BaseUserManager): | |
use_in_migrations = True | |
def _create_user(self, email, password, **extra_fields): | |
""" | |
Creates and saves a User with the given email and password. | |
""" | |
if not email: | |
raise ValueError('The given email must be set') | |
email = self.normalize_email(email) | |
user = self.model(email=email, **extra_fields) | |
user.set_password(password) | |
user.save(using=self._db) | |
return user | |
def create_user(self, email, password=None, **extra_fields): | |
extra_fields.setdefault('is_superuser', False) | |
return self._create_user(email, password, **extra_fields) | |
def create_superuser(self, email, password, **extra_fields): | |
extra_fields.setdefault('is_superuser', True) | |
if extra_fields.get('is_superuser') is not True: | |
raise ValueError('Superuser must have is_superuser=True.') | |
return self._create_user(email, password, **extra_fields) | |
from django.db import models | |
# from testapp.core.models import User | |
from django.conf import settings | |
class Course(models.Model): | |
slug = models.SlugField(max_length=100) | |
name = models.CharField(max_length=100) | |
# tutor = models.ForeignKey(User, on_delete=models.CASCADE) | |
""" | |
This is perfectly okay. But if you are creating a reusable app, | |
that you want to make available for the public, | |
it is strongly advised that you use the following strategy | |
""" | |
tutor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment