V
V
Vladislav Dudashvili2021-03-18 11:05:08
Django
Vladislav Dudashvili, 2021-03-18 11:05:08

What is the problem that I can't do the migration?

The task is to make a captcha for the site. The choice fell on django-simple-captcha 0.5.13 In the installation documentation there is an item to make a migration to the database after connecting the captcha to INSTALLED_APPS. I'm trying to make a migration and I get this error django.db.utils.IntegrityError: UNIQUE constraint failed: new__feedback_reviews.email Tell me what to do?

605308db47d13807811346.png

models.py

from django.db import models

        #Отзывы
class Reviews(models.Model): 
    email = models.EmailField(unique=True)
    name = models.CharField("Имя", max_length=100, db_index=True)
    text = models.TextField("Сообщение", max_length=5000)
    date_pub = models.DateTimeField(auto_now_add=True)


    def __str__(self):
        return self.name


forms.py

from django import forms
from django.db import models
from .models import Reviews
from django.core.exceptions import ValidationError


        #Форма отзывов
class ReviewForm(forms.ModelForm):


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

        widgets = {
            'text': forms.TextInput(attrs={'class': 'form-control'}),
            'name': forms.TextInput(attrs={'class': 'form-control'}),
            'email': forms.TextInput(attrs={'class': 'form-control'}),
        }


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


views.py

from django.shortcuts import render, redirect
from django.views.generic import ListView, DeleteView
from django.views.generic.base import View
from .models import *
from .forms import ReviewForm
from django.conf import settings
from g_recaptcha.validate_recaptcha import validate_captcha

        #Форма отзывов
class Add_Review(View):
    def get(self, request):
        form = ReviewForm()
        return render(request, 'reviews.html', context={'form':form})

        #Проверка на валидность
    def post(self, request):
        bound_form = ReviewForm(request.POST)

        if bound_form.is_valid():
            new_review = bound_form.save()
            return redirect('detail_review_url')
        return render(request, 'reviews.html', context={'form':bound_form})


def review_detail(request):
    review = Reviews.objects.all()
    return render(request, 'see_reviews.html', context={'review':review})

Answer the question

In order to leave comments, you need to log in

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

You set the unique=True property for email, when the migration started, there were several records with the same email in the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question