V
V
Vladislav Dudashvili2021-03-22 16:37:26
Django
Vladislav Dudashvili, 2021-03-22 16:37:26

__init__ error while creating form. What to do?

When creating a form, it turns out that I violate the DRY rule. I don't understand how to make a mixin for the clean_email method.
Even if I just break this rule and try to call the same method, I get an error: TypeError: __init__() got an unexpected keyword argument 'unique' Can you tell me how to make a mixin or get rid of this error?

forms.py

from django import forms
from phonenumber_field.modelfields import PhoneNumberField
from django.db import models
from django.core.exceptions import ValidationError
from snowpenguin.django.recaptcha3.fields import ReCaptchaField


from .models import Review


class ReviewForm(forms.ModelForm):
    captcha = ReCaptchaField()

    class Meta:
        model = Review
        fields = ['text', 'name', 'email', 'captcha']

        widgets = {
            'text': forms.TextInput(attrs={'class': 'form__review', 'type': 'text', 'placeholder': 'Напишите отзыв'}),
            'name': forms.TextInput(attrs={'class': 'review-form__item', 'type': 'text', 'placeholder': 'Имя'}),
            'email': forms.TextInput(attrs={'class': 'review-form__item', 'type': 'text', 'placeholder': 'email'}),
        }

    def clean_email(self):
        new_email = self.cleaned_data['email']
        if Review.objects.filter(email=new_email).exists():
            raise ValidationError('You cannot reuse the same email')
        return new_email


class Room_Reservation(forms.Form):
    name = forms.CharField(max_length=200, widget=forms.TextInput(attrs={'placeholder': 'Имя'}))
    last_name = forms.CharField(max_length=200, widget=forms.TextInput(attrs={'placeholder': 'Фамилия'}))
    email = forms.EmailField(unique=True, widget=forms.TextInput(attrs={'placeholder': 'email'}))
    phone = PhoneNumberField(null=False, blank=False, unique=True, widget=forms.TextInput(attrs={'placeholder': 'Номер Телефона'}))


    def clean_email(self):
        new_email = self.cleaned_data['email']
        if Review.objects.filter(email=new_email).exists():
            raise ValidationError('You cannot reuse the same email')
        return new_email

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-03-22
@bacon

I mixed everything together, in short, it writes to you that there is no such unique argument. That's where you got it from in forms.EmailField?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question