gpt4 book ai didi

python - Django IntegrityError 电子邮件不是唯一的

转载 作者:行者123 更新时间:2023-11-30 22:53:30 24 4
gpt4 key购买 nike

我正在与普通/ guest 用户一起处理我的结帐 View ,但很难解决完整性错误。想法是让访客用户仅使用电子邮件注册才能结帐,我需要将用户电子邮件设置为唯一。

模型.py

from django.conf import settings
from django.db import models

class UserCheckout(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, blank=True)
email = models.EmailField(unique=True)

def __unicode__(self):
return self.email

表单.py

from django import forms
from django.contrib.auth import get_user_model

User=get_user_model()
class GuestCheckoutForm(forms.Form):
email = forms.EmailField()
email2 = forms.EmailField(label='Verify Email')
def clean_email2(self):
email = self.cleaned_data.get("email")
email2 = self.cleaned_data.get("email2")
if email == email2:
user_exists = User.objects.filter(email=email).count()
if user_exists != 0:
raise forms.ValidationError("User already exists. Please login instead")
return email2
else:
raise forms.ValidationError("Please confirm emails addresses are the same.")

在我的购物车 View 中,这就是我呈现表单的方式。

def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
email = form.cleaned_data.get("email")
user_checkout = UserCheckout.objects.create(email=email)
return self.form_valid(form)
else:
return self.form_invalid(form)

我已经向管理员注册了该模型,并且在管理员中它显示了复制错误,但从前端我收到以下错误:

IntegrityError at /checkout/
column email is not unique
Request Method: POST
Request URL: http://localhost:8000/checkout/
Django Version: 1.8.13
Exception Type: IntegrityError
Exception Value:
column email is not unique
Exception Location: C:\Users\Ali\ecomm\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 318
Python Executable: C:\Users\Ali\ecomm\Scripts\python.EXE
Python Version: 2.7.9

最佳答案

每次结帐发生时,您都会创建一个新的UserCheckout。在所有这些条目中,只允许每封电子邮件仅存在一次。

我认为你不想要这个。因为如果客人点了两次,这是不允许的,因为他的电子邮件已经在数据库中。这就是您收到此错误的原因。

关于python - Django IntegrityError 电子邮件不是唯一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com