用户注册导致IntegrityError [英] User signup leads to IntegrityError

查看:288
本文介绍了用户注册导致IntegrityError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户注册后,将显示以下IntegrityError:

  / users / signup中的IntegrityError 
1062键2的重复输入1)

即使用户收到错误,他可以定期登录。用户被创建,以及UserProfile。



这是/ users / signup视图导致错误:



pre $ def注册(请求):
signup_form = SignupForm(request.POST)
#goal = request.POST.get('goal')
goal = None
如果signup_form.is_valid():
username = signup_form.cleaned_data ['username']
email = signup_form.cleaned_data ['email']
password = signup_form .cleaned_data ['password']
user = User.objects.create_user(username,email,password)
user = authenticate(username = username,password = password)
登录(请求,用户)
return HttpResponseRedirect(/)
else:
返回render_to_response(authorize.html,{'signup_form':signup_form,'goal':goal},context_instance = RequestContext ))

这是我的/user/models.py:

 从django.db导入模型
从django.contrib .auth.models import来自django.db.models.signals的user
import post_save
from main.models import Goal

class UserProfile(models.Model):
user = models.OneToOneField(User)
joined_goals = models.ManyToManyField(Goal,related_name =joined_goals)
followingGoals = models.ManyToManyField(Goal,related_name =following_goals)

def __unicode __(self):
return self.user.username

def get_goals(self):
try:
goals = Goal.objects.filter user = self.user)
返回目标
除了Goal.DoesNotExist:
return []

def create_user_profile(发件人,实例,创建,** kwargs):
如果创建:
userProfile = UserProfile.objects.create(user = instance)

post_save.connect(create_user_profile,sender = User)

我已将MySQL数据库中的所有表多次删除,并运行python manage.py syncdb,但我仍然得到相同的错误r / p>

编辑:我的本地主机上没有这个错误,只有在我的远程主机上。我在我的本地主机上使用sqlite,在远程主机上使用mysql。

解决方案

我要猜猜,建议可以作为您的post_save信号被多次注册,因为models.py被多次加载。您没有以安全的方式注册您的信号。也许它试图在save()上快速创建个人资料,并得到错误。



查看防止重复信号



尝试将最后一行更改为:

  post_save.connect(create_user_profile,sender = User,
dispatch_uid = user_create_profile)

发送uid将导致它只被注册一次,即使它被调用了作为导入的结果。


After a user signs up, the following IntegrityError is displayed:

IntegrityError at /users/signup
(1062, "Duplicate entry '1' for key 2")

Even though the user gets an error, he can log in regularly. The User gets created, along with the UserProfile.

Here is the /users/signup view that causes the error:

def signup(request):
  signup_form = SignupForm(request.POST)
  #goal = request.POST.get('goal')
  goal = None
  if signup_form.is_valid():
    username = signup_form.cleaned_data['username']
    email = signup_form.cleaned_data['email']
    password = signup_form.cleaned_data['password']
    user = User.objects.create_user(username, email, password)
    user = authenticate(username=username, password=password)
    login(request, user)
    return HttpResponseRedirect("/")
  else:
    return render_to_response("authorize.html", {'signup_form': signup_form, 'goal': goal}, context_instance=RequestContext(request))

Here is my /user/models.py:

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

class UserProfile(models.Model):
  user = models.OneToOneField(User)
  joined_goals = models.ManyToManyField(Goal, related_name="joined_goals")
  followingGoals = models.ManyToManyField(Goal, related_name="following_goals")

  def __unicode__(self):
    return self.user.username

  def get_goals(self):
    try:
      goals = Goal.objects.filter(user=self.user)
      return goals
    except Goal.DoesNotExist:
      return []

def create_user_profile(sender, instance, created, **kwargs):
  if created:
    userProfile = UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

I've dropped all of the tables in my MySQL database multiple times and run python manage.py syncdb, but I still get the same error everytime after creating a new user.

Edit: I do not get this error on my localhost, only on my remote host. I use sqlite on my localhost and mysql on my remote host.

解决方案

I'm going to take a guess and suggest it could be your post_save signal being registered multiple times as a result of the models.py being loaded more than once. You aren't registering your signal in a safe manner. Maybe its trying to create the profile more than once rapidly on save() and getting the error.

Take a look at the docs on Preventing Duplicate Signals

Try changing the last line to something like:

post_save.connect(create_user_profile, sender=User, 
                    dispatch_uid="user_create_profile")

Having the dispatch uid will cause it only to be registered once, even if its called many times as a result of imports.

这篇关于用户注册导致IntegrityError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆