0

我不明白我做错了什么!每次单击提交时,我都会收到此错误。这是一个简单的登记表

    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

我的模型::

      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, )

看法::

# 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))

如果我能得到一些帮助,那就太好了!提前致谢

4

1 回答 1

3

您的表单处理方法会创建一个用户和一个匹配的供应商。但是您在 User 上有一个保存后信号,它为该用户创建了一个供应商。这意味着您最终会尝试创建两个供应商,而 OneToOne 字段意味着每个用户一个供应商的唯一关系。

要么摆脱信号,要么将供应商创建代码从表单处理视图中取出。

于 2013-06-10T22:11:49.970 回答