Как исправить ошибку Django "слишком много значений для распаковки (ожидалось 2)"?
Весь день я пытался создать систему "отправка-проверка-код". Это выглядит следующим образом: serilizer.py
class RegisterationSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=True, help_text=_("Enter a valid password"))
repeated_password = serializers.CharField(write_only=True, required=True, help_text=_("Enter your password again"))
def to_internal_value(self, data):
email = data["email"]
user = User.objects.get(email=email)
if User.objects.filter(email=email, is_active=False).exists():
if VerficationCode.objects.filter(user=user).values("expire_date")[0]['expire_date'] < utc.localize(datetime.now()):
VerficationCode.objects.filter(user=user).delete()
verification_code = give_code()
VerficationCode.objects.create(user=user, code=verification_code)
send_mail("Verification Code", f"your code is: {verification_code}", "nima@gmail.com", [email])
return Response({"Message": "A verification code has been sent to your email"}, status=status.HTTP_200_OK)
elif VerficationCode.objects.filter(user=user).values("expire_date")[0]['expire_date'] >= utc.localize(datetime.now()):
raise serializers.ValidationError(_("Your verification code is still valid"))
return super().to_internal_value(data)
class Meta:
model = User
fields = ["email", "username", "name", "password", "repeated_password"]
extra_kwargs = {
'email': {'help_text': _("Enter your email")},
'username': {'help_text': _("Enter your username")},
'name': {'help_text': _("Enter your name"), 'required': False, 'allow_blank': True},
}
def validate(self, data):
print(User.objects.filter(email=data["email"], is_active=False))
if data["password"] and data["repeated_password"] and data["password"] != data["repeated_password"]:
raise serializers.ValidationError(_("Password and repeated password must be the same"))
if data["email"].strip() == data["username"].strip():
raise serializers.ValidationError(_("Email and username must be different"))
return data
Этот код переопределяет метод is_valid() в сериализаторе регистрации пользователя. Но когда я отправляю другой ответ с именем пользователя и паролем учетной записи, которая существует, но не активна, и первый внутренний if в функции to_interval... должен сработать, но он не сработал и возвращает ValueError в /api/authentication/register/ слишком много значений для распаковки (ожидалось 2); что мне теперь делать? полная ошибка трассировки: