我制作了自定义用户模型。
class CustomUserManager(BaseUserManager):
def _create_user(self, email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, is_active, is_admin, is_superuser):
if not email:
raise ValueError('Email must be set')
email = self.normalize_email(email)
user = self.model(
email=email,
username=username,
first_name=first_name,
last_name=last_name,
date_of_birth=date_of_birth,
gender=gender,
mobile_number=mobile_number,
date_joined=timezone.now,
is_active=is_active,
is_admin=is_admin,
is_superuser=is_superuser,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, **extra_fields):
user = self._create_user(email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, True, False, False, **extra_fields
)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, **extra_fields):
user = self._create_user(email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, True, True, True, **extra_fields )
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), max_length=254, unique=True)
username = models.CharField(_('user name'), max_length=254, unique=True)
first_name = models.CharField(_('first name'), max_length=30)
last_name = models.CharField(_('last name'), max_length=30)
date_of_birth = models.DateField()
gender = models.CharField(choices=GENDERTYPE, max_length=1)
mobile_number = models.IntegerField(unique=True)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'first_name', 'last_name', 'date_of_birth', 'gender', 'mobile_number']
def get_full_name(self):
return self.email
...
...
迁移成功完成。但是,在尝试创建超级用户时,我收到的错误是这样的:
(djangoweb) vagrant@precise32:/vagrant/djangoweb/myproject$ python manage.py createsuperuser
Email address: testme@gmail.com
User name: testme
First name: test
Last name: me
Date of birth: 1111-11-11
Gender: 1
Mobile number: 1111
Password:
Password (again):
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 52, in execute
return super(Command, self).execute(*args, **options)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 173, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "/vagrant/djangoweb/myproject/custom_user/models.py", line 72, in create_superuser
**extra_fields
File "/vagrant/djangoweb/myproject/custom_user/models.py", line 36, in _create_user
user.save(using=self._db)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 74, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/base.py", line 859, in _do_insert
using=using, raw=raw)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1059, in execute_sql
for sql, params in self.as_sql():
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1019, in as_sql
for obj in self.query.objs
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 958, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1461, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1440, in get_prep_value
value = super(DateTimeField, self).get_prep_value(value)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1296, in get_prep_value
return self.to_python(value)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1399, in to_python
parsed = parse_datetime(value)
File "/home/vagrant/Envs/djangoweb/local/lib/python2.7/site-packages/django/utils/dateparse.py", line 93, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or buffer
我认为这是因为与日期时间有关的事情,但我无法弄清楚出了什么问题。我在这里做错了什么?
答案 0 :(得分:2)
我不是肯定的,但我猜测了这一行:
user = self.model(
#other code
date_joined=timezone.now,
#other code
)
您将函数作为date_joined的值而不是实际值timezone.now()传递。这是在您拥有的模型声明中执行此操作的正确方法,但我相信您在制作对象时需要实际值。
user = self.model(
#other code
date_joined=timezone.now(),
#other code
)
当然,由于您指定了默认值,因此您可以将其完全保留,并让模型填写默认值。