Re: IntegrityError column email is not unique
@James As per my understanding I did following. Now it is not adding email if it exists but also its not giving me any error. class GuestCheckoutForm(forms.Form): email = forms.EmailField() email2 = forms.EmailField(label='Verify Email') def clean_email(self): email = self.cleaned_data["email"] if UserCheckout.objects.filter(email=email).exists(): raise forms.ValidationError("Please confirm emails addresses are the same.") return email def clean(self): cleaned_data = super(GuestCheckoutForm, self).clean() email = cleaned_data.get('email') email2 = cleaned_data.get('email2') if email and email2 and email != email2: self.add_error('email2', forms.ValidationError('Please confirm emails addresses.')) Rest of the code is same. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a63b8508-063d-405c-a7a2-67ab428bea21%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: IntegrityError column email is not unique
@James As per my understanding I did following. Now it is not adding email if it exists but also its not giving me any error. class GuestCheckoutForm(forms.Form): email = forms.EmailField() email2 = forms.EmailField(label='Verify Email') def clean_email(self): email = self.cleaned_data["email"] if UserCheckout.objects.filter(email=email).exists(): raise forms.ValidationError("Please confirm emails addresses are the same.") return email def clean(self): cleaned_data = super(GuestCheckoutForm, self).clean() email = cleaned_data.get('email') email2 = cleaned_data.get('email2') if email and email2 and email != email2: self.add_error('email2', forms.ValidationError('Please confirm emails addresses.')) Rest of the code is same. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/12116f4b-01b5-4a33-9397-20819d11557d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: IntegrityError column email is not unique
Thanks james for your kind response. Intention is to set a single identifier for guest user account email. If that email exists then user can login or if doesn't he will have to get registered. However let me implement your suggested code and I will respond you back. Thanks again -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a9ffd1b7-cd56-4cf6-853f-66409c700074%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: IntegrityError column email is not unique
On Jul 2, 2016 7:23 AM, "M Hashmi" wrote: > > I am working on my Checkout view with regular/guest user but getting hard time to come around the integrity error. Idea is to let guest users register with email only to checkout and I need to set the user email unique. > > models.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 > > > forms.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: This should probably be if UserCheckout.objects.filter(email=email).exists(): Do not use count() for checking whether or not a record exists. You are also checking against User objects, and not against UserCheckout objects, which doesn't make any sense based on the code you've posted. > raise forms.ValidationError("User already exists. Please login instead") > return email2 > else: > raise forms.ValidationError("Please confirm emails addresses are the same.") > > > In my cart views this is how I've rendered my form. > > > 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) Here is where the actual error is coming from. Your validation only checks User for a unique email constraint, but then you create a UserCheckout object without first checking the unique email constraint for UserCheckout. > return self.form_valid(form) > else: > return self.form_invalid(form) > > > I've registered the model with admin and in admin it shows the error for duplication perfectly fine but from frontend I am getting error below: > > > 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 > You'll only get this error if a user checks out using an email address that matches an existing user, not an existing email that's been used to check out before. Is the intention to create both UserCheckout and User objects (which can log in) through this process? -James -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVbUJ_P0jFAeybUZPifEsbO7sE%3Dz%3DSU9%2BACdfDb_PX49g%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
IntegrityError column email is not unique
I am working on my Checkout view with regular/guest user but getting hard time to come around the integrity error. Idea is to let guest users register with email only to checkout and I need to set the user email unique. models.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* forms.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.")* In my cart views this is how I've rendered my form. *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)* I've registered the model with admin and in admin it shows the error for duplication perfectly fine but from frontend I am getting error below: *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* Please advise. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b85b393e-1164-4d81-9269-5db4b287d64f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.