【发布时间】:2020-07-22 18:19:31
【问题描述】:
我正在使用djangorestframework-simplejwt 进行身份验证。我的用例要求我使用 OTP 而不是密码。
为了存储 OTP,我创建了以下模型:
class OneTimePassword(models.Model):
otp = models.IntegerField()
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
用户模型:
class User(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
country_code = models.IntegerField(default=91, max_length=3)
mobile = models.IntegerField(max_length=11, unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['mobile']
objects = CustomUserManager()
def __str__(self):
return f"{self.email},{self.mobile}"
我的计划是:
- POST
mobile到/api/generate_otp/端点 - 在此端点的视图中,会生成一个新的 OTP,并将其存储在
OneTimePassword模型中。 (用户由手机号确定) - 在
api/token/obtain/端点发布OTP 和手机号码。 - 如果 OTP 与存储在 OTP 模型中的值匹配,则返回 JWT 令牌。
第 2 步遇到困难,即无法通过用户更新 OneTimePassword 模型中的 OTP 值。
我尝试了以下方法:
class GenerateOTPMobileView(APIView):
permission_classes = ()
def post(self, request,):
mobile = request.data.get("mobile")
user = User.objects.get(mobile=mobile)
random_otp = randint(10000, 99999)
if user:
user.onetimepassword_set.otp = random_otp # This is not working
...
# send OTP through third party API
...
return Response({"success": "OTP sent to mobile number"},)
else:
return Response({"error": "Wrong Credentials"}, status=status.HTTP_400_BAD_REQUEST)
【问题讨论】:
-
它可能工作得很好,但是你在拯救用户吗?等等,你正在分配一个反向关系......如果这是 Django 2,它会被默默地忽略。使用these methods。
-
@Melvyn 我不知道该怎么做。另外,我不是在创建新用户,而是在更新 OTP 值
-
@Melvyn 任何帮助将不胜感激!
标签: django django-models django-rest-framework django-views django-rest-framework-simplejwt