【问题标题】:How to change default 'username' in django-graphql-jwt如何在 django-graphql-jwt 中更改默认的“用户名”
【发布时间】:2019-07-11 12:12:29
【问题描述】:
在 django-graphql-jwt 中查询 tokenAuth 需要两个字段:username 和 password,但我想使用 phone 或 email 而不是 username。我必须更改库的源代码吗?
【问题讨论】:
标签:
python
django
graphql
【解决方案1】:
如果您将 Django 的 AbstractBaseUser 子类化以创建您的自定义 User 模型,那么您将必须提供 USERNAME_FIELD 并将其设置为您想要用于登录的任何内容,例如 USERNAME_FIELD = 'email'。您可以阅读有关 USERNAME_FIELD here 的信息。然后您可以执行以下操作:
mutation {
tokenAuth(email: "youremail@gmail.com", password: "password") {
token
}
}
确保您尝试登录的用户的is_active 字段设置为TRUE,因为 Django 的默认身份验证类会在身份验证时检查它是否设置为这样(除非您已使用自定义身份验证覆盖此行为方法)。希望这会有所帮助。
【解决方案2】:
django-graphql-auth 使用 django-graphql-jwt 进行身份验证,并具有适用于用户名、电子邮件或辅助电子邮件的默认登录突变。
它适用于默认用户模型或自定义用户模型。
来自docs的示例
mutation {
tokenAuth(
# username or email
email: "skywalker@email.com"
password: "123456super"
) {
success,
errors,
token,
refreshToken,
unarchiving,
user {
id,
username
}
}
}