managers.py 1.2 KB

12345678910111213141516171819202122232425
  1. from django.contrib.auth.base_user import BaseUserManager
  2. from django.utils.translation import gettext_lazy as _
  3. class UserManager(BaseUserManager):
  4. def create_user(self, firstname, lastname, email, phone_number, password, **other_fields):
  5. if not email:
  6. raise ValueError(_('You must provide an email address'))
  7. email = self.normalize_email(email)
  8. user = self.model(firstname=firstname, lastname=lastname, email=email, phone_number=phone_number, **other_fields)
  9. user.set_password(password)
  10. user.save()
  11. return user
  12. def create_superuser(self, firstname, lastname, email, phone_number, password, **other_fields):
  13. other_fields.setdefault('is_staff', True)
  14. other_fields.setdefault('is_superuser', True)
  15. other_fields.setdefault('is_active', True)
  16. if other_fields.get('is_staff') is not True:
  17. raise ValueError(_('Superuser must have is_staff=True.'))
  18. if other_fields.get('is_superuser') is not True:
  19. raise ValueError(_('Superuser must have is_superuser=True.'))
  20. return self.create_user(firstname, lastname, email, phone_number, password, **other_fields)