0

I dont understand what I am doing wrong!! I am getting this error everytime i click on submit. This is a simple registration form

    Request Method: POST
    Request URL:    http://127.0.0.1:8000/register/
    Django Version: 1.5.1
    Exception Type: IntegrityError
    Exception Value:    
    column user_id is not unique
    Exception Location: /home/xxxx/Desktop/forfte/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py in execute, line 362
    Python Executable:  /home/xxx/Desktop/forfte/bin/python
    Python Version: 2.7.3

My Models::

      from django.db import models
      from django.db.models.signals import post_save
      from django.contrib.auth.models import User


      class Vendor(models.Model):
        user= models.OneToOneField(User)
        def __unicode__ (self):
         return self.user   


     def create_vendor_user_callback(sender, instance, **kwargs):
          vreg, new=Vendor.objects.get_or_create(user=instance)

      post_save.connect(create_vendor_user_callback,User, )

View::

# Create your views here.
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.template import RequestContext
from vreg.forms import RegistrationForm
from vreg.models import Vendor
from django.contrib.auth import authenticate

def VendorRegistration(request):
    if request.user.is_authenticated():
        return HttpRequestRedirect('/profile/')
    if request.method=='POST':
        form= RegistrationForm(request.POST)
        if form.is_valid():
            print "i am in"
            print type(form.cleaned_data['emailadd'])
            print type(form.cleaned_data['username'])
            user=User.objects.create_user(username=form.cleaned_data['username'], email= form.cleaned_data['emailadd'], password= form.cleaned_data['password'])
            user.save()
            vreg=Vendor(user=user)
            vreg.save()
            return HttpResponseRedirect('/profile/')
        else:
            return render_to_response('register.html', {'form':form}, context_instance=RequestContext(request))

    else:
        ''' user is not submitting form show them blank registration form'''
        form= RegistrationForm()
        context={'form':form}
        return render_to_response('register.html', context, context_instance=RequestContext(request))

Would be really nice if I could get some Help!! Thanks in advance

1 Answer 1

3

Your form processing method creates a User and a matching Vendor. But you also have a post-save signal on User that creates a Vendor for that user. That means you end up trying to create two vendors, whereas a OneToOne field implies a unique relationship of one vendor per user.

Either get rid of the signal, or take the Vendor creation code out of the form processing view.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.