2016-07-22 4 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

난 긍정적 아니에요하지만 난 그에게이 줄을 추측하고있어 : 대신 실제 값 timezone.now의 date_joined 당신은 값으로 함수를 전달하는

user = self.model(
    #other code 
    date_joined=timezone.now, 
    #other code 
) 

(). 그것은 당신이 가진 모델 선언에서 그것을하는 올바른 방법입니다,하지만 당신이 객체를 만들 때 실제 가치가 필요하다고 생각합니다.

user = self.model(
    #other code 
    date_joined=timezone.now(), 
    #other code 
) 

물론 기본값을 지정 했으므로이 값을 완전히 그대로두고 모델을 기본값으로 채울 수 있습니다.

+0

예, 그게 전부입니다! 나는 어리 석다. 정말 고맙습니다. – Robin