Answer the question
In order to leave comments, you need to log in
How to create a UserManager for a bulky custom user?
class User(AbstractBaseUser, PermissionsMixin):
phone = models.CharField(
max_length=15,
unique=True,
)
sms = models.BooleanField(default=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False)
admin = models.BigAutoField(default=False)
tutor = models.ForeignKey(
'self',
on_delete=models.SET_NULL,
blank=True,
null=True,
)
TRAINING_ONLINE = 'ON'
TRAINING_DISTANCE = 'DT'
TRAINING_FULLTIME = 'FT'
TRAINING_NONE = 'NN'
TRAINING_CHOICES = (
(TRAINING_ONLINE, 'Online'),
(TRAINING_DISTANCE, 'Distance'),
(TRAINING_FULLTIME, 'Fulltime'),
(TRAINING_NONE, 'None'),
)
training = models.CharField(
max_length=2,
choices=TRAINING_CHOICES,
default=TRAINING_NONE,
)
first_name = models.CharField(
max_length=50,
blank=False,
)
last_name = models.CharField(
max_length=50,
blank=True,
null=True,
)
country = models.CharField(
max_length=50,
blank=True,
)
city = models.CharField(
max_length=50,
blank=True,
)
EFFICIENCY_VERY_LOW = '1'
EFFICIENCY_LOW = '2'
EFFICIENCY_MEDIUM = '3'
EFFICIENCY_HIGH = '4'
EFFICIENCY_VERY_HIGH = '5'
EFFICIENCY_CHOICES = (
(EFFICIENCY_VERY_LOW, 'Very low'),
(EFFICIENCY_LOW, 'Low'),
(EFFICIENCY_MEDIUM, 'Medium'),
(EFFICIENCY_HIGH, 'High'),
(EFFICIENCY_VERY_HIGH, 'Very high'),
)
efficiency = models.CharField(
max_length=1,
choices=EFFICIENCY_CHOICES,
default=EFFICIENCY_MEDIUM,
)
USERNAME_FIELD = 'phone'
REQUIRED_FIELDS = ['first_name']
def __str__(self):
return self.phone
def get_full_name(self):
return self.first_name + ' ' + self.last_name
def get_short_name(self):
return self.first_name
@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
@property
def is_active(self):
return self.active
class UserManager(BaseUserManager):
def create_user(self, phone=None, first_name=None, password=None,
sms=True, active=True, staff=False, admin=False,
tutor=None, training='NN', efficiency='3',
last_name=None, country=None, city=None):
if not phone:
raise ValueError('Users must have a phone number.')
if not first_name:
raise ValueError('Users must have at least first name.')
if not password:
raise ValueError('Users must have a password.')
user = self.model(
phone=phone,
first_name=first_name,
sms=sms,
active=active,
staff=staff,
admin=admin,
tutor=tutor,
training=training,
efficiency=efficiency,
last_name=last_name,
country=country,
city=city
)
user.set_password(password)
user.save(using=self._db)
return user
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question