Created
March 15, 2017 14:41
-
-
Save pyaf/b527ef61245e9019d6cd12a6d89d63e7 to your computer and use it in GitHub Desktop.
UserManager class
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 django.contrib.auth.models import UserManager | |
class UserManager(UserManager): | |
use_in_migrations = True | |
def _create_user(self, phone_number, password, **extra_fields): | |
if not phone_number: | |
raise ValueError('The given phone number must be set') | |
user = self.model(phone_number=phone_number, **extra_fields) | |
user.set_password(password) | |
user.save(using=self._db) | |
return user | |
def create_user(self, phone_number, password=None, **extra_fields): | |
extra_fields.setdefault('is_superuser', False) | |
return self._create_user(phone_number, password, **extra_fields) | |
def create_superuser(self, phone_number, password, **extra_fields): | |
extra_fields.setdefault('is_superuser', True) | |
extra_fields.setdefault('is_staff', True) | |
if extra_fields.get('is_superuser') is not True: | |
raise ValueError('Superuser must have is_superuser=True.') | |
return self._create_user(phone_number, password, **extra_fields) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment