Been messing with django 1.5 by creating my custom user model. In my model, I have a required field dob = DateTimeField(). I've kept pretty much the UserManager and the AbstractUser the same. However, when I try to python manage.py createsuperuser, I don't input the dob and I get the error:
IntegrityError: null value in column "dob" violates not-null constraint
How do I edit UserManager to allow me to input the date of birth in the terminal and I do I configure the input to store as DateTimeField in the db?
Edit my answer:
class UserManager(BaseUserManager):
def create_user(self, username, email=None, password=None, dob=None, **extra_fields):
"""
Creates and saves a User with the given username, email and password.
"""
now = timezone.now()
if not username:
raise ValueError('The given username must be set')
if not email:
raise ValueError('Email must be given')
email = UserManager.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=False, is_active=True, is_superuser=False,
last_login=now, date_joined=now, dob=dob, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password, dob, **extra_fields):
u = self.create_user(username, email, password, dob, **extra_fields)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.save(using=self._db)
return u
# ... in the user model:
...
REQUIRED_FIELDS = ['email', 'dob']
...
You need to overwrite REQUIRED_FIELDS
in the model that inherits from AbstractUser
. Browsing through contrib/auth/models.py
one can notice how AbstractBaseUser
has REQUIRED_FIELDS = []
and AbstractUser
has REQUIRED_FIELDS = ['email']
. My guess would be you should have something like:
class MyCoolUserModel(AbstractUser):
REQUIRED_FIELDS = ['email', 'dob']
...
EDIT: Not a guess any more. It works.
Generally speaking you can look what createsuperuser
does by having a look at contrib/auth/management/commands/createsuperuser.py
. Any non-auth command can be located at core/management/commands/
. Pretty much any manage.py
issue can be resolved by having a look at the source of the command.