【问题标题】:Django TypeError: force_authenticate() missing 1 required positional argument: 'self'Django TypeError:force_authenticate()缺少1个必需的位置参数:'self'
【发布时间】:2021-04-15 11:01:22
【问题描述】:

测试用例 setUp() 首先创建一个用户并尝试强制对用户进行身份验证,但发生有线错误...

TypeError:force_authenticate() 缺少 1 个必需的位置参数:'self'

class PrivateUserApiTest(TestCase):
    """ Test API that require authentication """

    def setUp(self):
        self.user = create_user(
            email="test@test.com",
            password="testpass",
            name="testName"
        )
        print(self.user)
        self.client = APIClient
        self.client.force_authenticate(user=self.user) # <-- 

【问题讨论】:

  • 应该有self.client=APIClient()
  • 先检查你的force_authenticate方法是否有第一个参数self,如:def force_authenticate(self, user, other, arguments):

标签: python django django-models django-rest-framework tdd


【解决方案1】:

正如@BhavyaParikh 所说,而不是这样:

self.client = APIClient

这样做:

self.client = APIClient()

【讨论】:

    猜你喜欢
    • 2017-07-27
    • 1970-01-01
    • 2021-12-15
    • 2023-02-06
    • 1970-01-01
    • 2021-09-15
    • 2013-07-06
    • 2022-01-05
    相关资源
    最近更新 更多