django在重复键错误上保存覆盖

时间:2012-12-05 05:34:38

标签: django forms

我正在覆盖modelForm的保存方法

  def save(self, commit=True, *args, **kwargs):                                                                                     
     userProfile = super(UserProfileForm, self).save(*args, **kwargs)                                                              
     if self.cleaned_data.get('birth_year') :                                                                                      
          userProfile.birthDay=date(self.cleaned_data['birth_year'], self.cleaned_data['birth_month'], self.cleaned_data['birth_day'])           
     **userProfile.save(commit)** <- This is error!!!
     return userProfile                                                                                                          

这是view.py

def user(request):                                                                                                                                                                                                                                                            
    if request.method=='POST':                                                                                                                                                                                                                                                
        form = UserProfileForm(request.POST, instance=request.user.get_profile(), option='modify')                                                                                                                                                                            

        if form.is_valid():                                                                                                                                                                                                                                                   
            userProfile = form.save()                                                                                                                                                                                                                                         
    else:                                                                                                                                                                                                                                                                     
        form = UserProfileForm(instance = request.user.get_profile(), option='modify')                                                                                                                                                                                        

    return render(request,'profile/user.html', {'userProfileForm':form,})                                                                                                                                                                                                     

但是如果我更新了我的UserProfile,那么form.save()会因为重复的密钥而产生错误。

我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

save(commit)会强行插入。

commit是关键字参数。

save(commit=commit)