Skip to content

Instantly share code, notes, and snippets.

@pyaf
Created March 15, 2017 14:41
Show Gist options
  • Save pyaf/b527ef61245e9019d6cd12a6d89d63e7 to your computer and use it in GitHub Desktop.
Save pyaf/b527ef61245e9019d6cd12a6d89d63e7 to your computer and use it in GitHub Desktop.
UserManager class
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