非默认参数遵循默认参数

时间:2016-08-01 12:41:13

标签: django django-models django-managers

在以下代码中,我收到一条错误消息:Non-default argument follows default argument

有人会碰巧知道如何解决这个问题吗?

class MyUserManager(BaseUserManager):
    def _create_user(self, email, first_name=None, last_name=None, username=None,
                     password, is_staff, is_superuser, **extra_fields):
        now = timezone.now()

        if not email:
            raise ValueError('Users must have an email.')

        email = self.normalize_email(email)

        if first_name and last_name:
            potential_username = slugify(
                "{0} {1}".format(first_name, last_name))

            try:
                check_user = self.model.get(
                    username=potential_username).exists()
            except check_user.DoesNotExist:
                check_user = None

            if not check_user:
                username = potential_username

        user = self.model(email=email,
                          username=username,
                          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, first_name, last_name, username,
                    password=None, **extra_fields):
        return self._create_user(email, password, is_staff=False,
                                 is_superuser=False, **extra_fields)

    def create_superuser(self, email, username, password, **extra_fields):
        return self._create_user(email, password, is_staff=True,
                                 is_superuser=True, **extra_fields)

1 个答案:

答案 0 :(得分:1)

这是不允许的

def _create_user(self, email, first_name=None, last_name=None, username=None,
                 password, is_staff, is_superuser, **extra_fields):

您将默认值分配给first_namelast_nameusername,但不会分配给passwordstaff等。您必须更改订单或您需要为使用用户名的参数指定默认值。

示例:

def _create_user(self, email, 
 password, is_staff, is_superuser,
 first_name=None, last_name=None,
 username=None,**extra_fields):