Django:使用AbstractBaseUser和BaseUserManager保存用户时出错

时间:2015-09-16 01:29:09

标签: python django

我创建了一个自定义用户模型,并尝试创建一个新用户。我在shell中尝试了manage.py createsuperuser以及我的代码中的User.objects.create_user(email, password)。两者都会导致相同的错误:

$ ./manage.py createsuperuser
Email: person@example.com
Is manager: True
Password: 
Password (again): 
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 50, in execute
    return super(Command, self).execute(*args, **options)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 149, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "/home/martin/projects/p34b2.0/projects/basedin-2.0/apps/accounts/models.py", line 40, in create_superuser
    user = self.create_user(email, password, **kwargs)
  File "/home/martin/projects/p34b2.0/projects/basedin-2.0/apps/accounts/models.py", line 36, in create_user
    user.save()
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/base.py", line 710, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/base.py", line 738, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/base.py", line 803, in _save_table
    forced_update)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/base.py", line 833, in _do_update
    filtered = base_qs.filter(pk=pk_val)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/query.py", line 679, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/query.py", line 697, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1309, in add_q
    clause, require_inner = self._add_q(where_part, self.used_aliases)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1337, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1208, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1101, in build_lookup
    return final_lookup(lhs, rhs)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/lookups.py", line 101, in __init__
    self.rhs = self.get_prep_lookup()
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/lookups.py", line 139, in get_prep_lookup
    return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 727, in get_prep_lookup
    return self.get_prep_value(value)
  File "/home/martin/projects/p34b2.0/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'person@example.com'

我的模特/经理:

class UserManager(BaseUserManager):
    def create_user(self, email, password, **kwargs):
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(self.normalize_email(email), **kwargs)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password, **kwargs):
        user = self.create_user(email, password, **kwargs)
        user.is_admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser, PermissionsMixin, EditMixin):
    email = models.EmailField(max_length=255, unique=True)
    business_access = models.ManyToManyField(BusinessAccess, blank=True)
    auth_key = models.UUIDField(unique=True, max_length=64, default=uuid.uuid4)
    is_manager = models.BooleanField(default=False)

    REQUIRED_FIELDS = ['is_manager']
    USERNAME_FIELD = 'email'
    objects = UserManager()

    class Meta:
        verbose_name = "User"
        verbose_name_plural = "Users"

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

    @property
        def is_staff(self):
    return self.is_superuser

我做错了吗?

2 个答案:

答案 0 :(得分:0)

尝试使用Manager

中的create_user方法
def _create_user(self, email, password,
                 is_staff, is_superuser, **extra_fields):
    """
    Creates and saves a User with the given email and password.
    """
    now = timezone.now()
    if not email:
        raise ValueError('The given email must be set')
    email = self.normalize_email(email)
    user = self.model(email=email,
                      is_staff=is_staff, is_active=True,
                      is_superuser=is_superuser, last_login=now,
                      date_joined=now, **extra_fields)
    user.set_password(password)
    user.save(using=self._db)
    return user

def create_user(self, email, password=None, **extra_fields):
    return self._create_user(email, password, False, False,
                             **extra_fields)

def create_superuser(self, email, password, **extra_fields):
    return self._create_user(email, password, True, True,
                             **extra_fields)

我尝试了你的例子并遇到了同样的错误。这是我的实现,没有任何问题正确工作

答案 1 :(得分:0)

您需要写信,这对我有帮助:

self.model(email=self.normalize_email(email), **kwargs)