查询tokenAuth
需要两个字段:username
和password
,但是我想使用phone
或email
而不是{ {1}}。我必须更改库的源代码吗?
答案 0 :(得分:1)
如果您要继承Django的class Orders(models.Model):
objects = jmodels.jManager()
product = models.ForeignKey('Products', models.DO_NOTHING)
user_phone = models.ForeignKey('Users', models.DO_NOTHING, db_column='user_phone')
order_date = jmodels.jDateField()
status = models.CharField(max_length=11)
price = models.CharField(max_length=11)
count = models.IntegerField()
class Meta:
managed = False
db_table = 'Orders'
verbose_name_plural = "user orders"
def __str__(self):
return '%s------- (%s)' % (self.user_phone,self.status)
class Users(models.Model):
objects = jmodels.jManager()
phone = models.CharField(unique=True, max_length=11)
name = models.CharField(max_length=80)
family = models.CharField(max_length=80)
nationalcode = models.CharField(max_length=11, blank=True, null=True)
city = models.CharField(max_length=80, blank=True, null=True)
address = models.TextField(blank=True, null=True)
profilepic = models.TextField(blank=True, null=True)
profiletext = models.TextField(blank=True, null=True)
bons = models.CharField(max_length=11, blank=True, null=True)
charge = models.CharField(max_length=11, blank=True, null=True)
registerdate = jmodels.jDateField()
status = models.CharField(max_length=11, blank=True, null=True)
class Meta:
managed = False
db_table = 'Users'
verbose_name_plural = "Users"
def __str__(self):
return '%s %s ---- (%s)' % (self.name,self.family,self.phone)
以创建自定义AbstractBaseUser
模型,则必须提供User
并将其设置为您要用于登录的任何内容,USERNAME_FIELD
。您可以阅读有关USERNAME_FIELD here的信息。然后您可以执行以下操作:
USERNAME_FIELD = 'email'
请确保您要登录的用户的mutation {
tokenAuth(email: "youremail@gmail.com", password: "password") {
token
}
}
字段设置为is_active
,因为Django的默认身份验证类会在身份验证时检查是否已将其设置为这样(除非您已覆盖此行为)使用您的自定义身份验证方法)。希望这会有所帮助。
答案 1 :(得分:0)
django-graphql-auth使用django-graphql-jwt进行身份验证,并具有适用于用户名,电子邮件或辅助电子邮件的默认登录突变。
它与默认用户模型或自定义模型一起使用。
中的示例mutation {
tokenAuth(
# username or email
email: "skywalker@email.com"
password: "123456super"
) {
success,
errors,
token,
refreshToken,
unarchiving,
user {
id,
username
}
}
}