0

modelForm の save メソッドをオーバーライドしています

  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() でエラーが発生します。

どうすればこの問題を解決できますか?

4

1 に答える 1

1

save(commit)挿入を強制します。

commitはキーワード引数です。

save(commit=commit)

于 2012-12-06T04:39:40.290 に答える