非默认参数跟随默认参数

Non-default argument follows default argument

在以下代码中,我收到一条错误消息: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)

这是不允许的

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):