2016-07-22 94 views
0

我做了一個自定義用戶模型。自定義用戶模型 - TypeError:預期的字符串或緩衝區

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) [email protected]:/vagrant/djangoweb/myproject$ python manage.py createsuperuser 
Email address: [email protected] 
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 

我想這是因爲涉及到日期時間的東西,但我無法弄清楚什麼是錯的。我在這裏做錯了什麼?

回答

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 
) 

當然,由於您有一個默認的指定,您可以始終完全放棄這一點,讓模型填寫默認值。

+0

是的,就是這樣!我一直是個傻瓜。非常感謝。 – Robin

相關問題