首页 > 解决方案 > create_user() 缺少 1 个必需的位置参数:“密码”

问题描述

我正在使用电子邮件而不是用户名进行身份验证,并且我尝试添加谷歌身份验证一切正常,但这是我得到的错误:

File "/home/tboss/Desktop/environment/liveimages/backend/venv/lib/python3.7/site-packages/social_django/storage.py", line 79, in create_user
user = cls.user_model().objects.create_user(*args, **kwargs)
TypeError: create_user() missing 1 required positional argument: 'password'

用户模型:

class UserManager(BaseUserManager):
      def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
          if not email:
             raise ValueError('user must have email address')
          now = timezone.now()
          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, **extra_fields):
         return self._create_user(email, password, False, False, **extra_fields)

     def create_superuser(self, email, password, **extra_fields):
         user=self._create_user(email, password, True, True, **extra_fields)
         user.save(using=self._db)
         return user

class CustomUser(AbstractBaseUser, PermissionsMixin):
      email = models.EmailField(max_length = 100, unique = True)
      First_Name = models.CharField(max_length = 100, null = True, blank = True)
      Last_Name = models.CharField(max_length = 100, null = True, blank = True)
      is_staff = models.BooleanField(default = False)
      is_superuser = models.BooleanField(default = False)
      is_active = models.BooleanField(default = True)
      last_login = models.DateTimeField(null = True, blank = True)
      date_joined = models.DateTimeField(auto_now_add=True)

      USERNAME_FIELD = "email"
      EMAIL_FIELD = "email"
      REQUIRED_FIELD = []

     objects = UserManager()

     def get_absolute_url(self):
         return "/users/%i/" % (self.pk)

还建议我如何与反应集成

标签: djangopython-3.xgoogle-oauth

解决方案


正如您的日志所述:create_user除了您的身份验证方法未提供的密码。以下更改应该可以解决问题:

class UserManager(BaseUserManager):
      def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
          if not email:
             raise ValueError('user must have email address')
          now = timezone.now()
          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
                   )
        # We check if password has been given
        if password:
            user.set_password(password)
         user.save(using=self._db)
         return user

     #We change following functions signature to allow "No password"
     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=None, **extra_fields):
         user=self._create_user(email, password, True, True, **extra_fields)
         user.save(using=self._db)
         return user

推荐阅读