Получение данных из запроса при создании объекта в django rest framework
Я пытаюсь проверить электронную почту путем отправки кода, вот как я подхожу к проблеме: я создал модель Email_for_Verification с двумя полями (email , код) код генерируется случайным образом, я создаю экземпляр этой модели, когда пользователь вводит свой email, я отправляю сгенерированный код по email, на втором этапе пользователь вводит полученный код, и я перехожу к проверке с помощью View под названием: Verify_code. но я сталкиваюсь с ошибкой. Я сталкиваюсь с этой ошибкой при попытке отправить электронное письмо пользователю при создании экземпляра Email_for_Verification
'Request' object has no attribute 'email'
Вот мой код
Views.py
class Verify_code(APIView):
def post(self, request):
data = request.data
code = data.get('code')
email = data.get('email')
email_for_verification = models.Email_for_Verification.objects.get(email=email)
if code == email_for_verification.code:
return Response({"valid" : "Valid code", "email": email}, status=status.HTTP_200_OK)
else :
return Response({"invalid" : "Invalid code", "email": email}, status=status.HTTP_404_NOT_FOUND)
class EmailForVerificationView(CreateAPIView):
queryset = models.Email_for_Verification.objects.all()
serializer_class = EmailForVerificationSerializer
def create(self, request):
created = models.Email_for_Verification.objects.create(email = request.email).save()
if created:
data = request.data
email = data['email']
code = data['code']
send_mail(
'He is the code of verification',
code,
'dummy@mail.com',
[email],
fail_silently=True,
)
Models.py
def generate_activation_code():
return int(''.join([str(random.randint(0,10)) for _ in range(6)]))
class Email_for_Verification(models.Model):
email = models.EmailField(
max_length=100, verbose_name='email', unique=True)
code = models.CharField(max_length=6, default=generate_activation_code)
Serializers.py
class EmailForVerificationSerializer(serializers.ModelSerializer):
class Meta:
model = Email_for_Verification
fields = '__all__'
created = models.Email_for_Verification.objects.create(email=request.email).save()
Должно быть:
created = models.Email_for_Verification.objects.create(email=request.data['email'])
Поле email отправляется в request.data
нет в request