get_or_create throws Integrity Error

匿名 (未验证) 提交于 2019-12-03 03:05:02

问题:

Given that the whole point of object.get_or_create() is to get the object if it already exists, I fail to see why it's throwing an integrity error for this code:

class UserAdd(TemplateView):  def post(self, request, *args, **kwargs):     context = self.get_context_data(*args, **kwargs)     form = UserAddForm(request.POST)     if form.is_valid():         first_name = form.cleaned_data['first_name']         last_name = form.cleaned_data['last_name']         myemail = form.cleaned_data['email']         mypass = form.cleaned_data['password']         if myemail and mypass:             myuser,created = User.objects.get_or_create(email=myemail, username=myemail, first_name=first_name, last_name=last_name)             if created:                 myuser.set_password(mypass)     return HttpResponseRedirect('/') 

Here is the error:

django.db.utils.IntegrityError IntegrityError: (1062, "Duplicate entry 'user@domain.com' for key 'username_UNIQUE'") 

Anyone know what's going on?

回答1:

The parameters sent into the get_or_create method need to match exactly, or django's ORM would try to create a new object, and since a primary key/unique column constraint would be violated, you are getting the error.

Try this:

if form.is_valid():     first_name = form.cleaned_data['first_name']     last_name = form.cleaned_data['last_name']     myemail = form.cleaned_data['email']     mypass = form.cleaned_data['password']     if myemail and mypass:         myuser,created = User.objects.get_or_create(email=myemail, defaults = {'username': myemail, 'first_name': first_name, 'last_name': last_name})         if created:             myuser.set_password(mypass)  return HttpResponseRedirect('/') 

Read more on get_or_create here. The defaults= argument is what you needed.



回答2:

You are asking django to fetch a record based on four conditions:

  • email
  • username
  • first_name
  • last_name

So all four fields combined does not have a record.

You should do:

myuser, created = User.objects.get_or_create(    username=myemail, defaults={'first_name': first_name, 'last_name': last_name, 'email': myemail}) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!