JD Yang
JD Yang

Reputation: 371

django save override on duplicated key error

I'm overriding save method for 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                                                                                                          

This is 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,})                                                                                                                                                                                                     

But if I update my UserProfile, form.save() make error, for duplicated Key.

How Can I solve this problem?

Upvotes: 0

Views: 363

Answers (1)

save(commit) will force an insert.

commit is a keyword argument.

save(commit=commit)

Upvotes: 1

Related Questions