How to update django MultiModelForm? Please HELP

115 views
Skip to first unread message

Sunil Kothiyal

unread,
Jun 25, 2018, 9:21:14 AM6/25/18
to Django users
Hello All,

 I had created multiModelForm in Django, I can view, and save data, I don't know how to Update data into the database. I want if user id exists in database then update else insert.exist in the database then update else insert.

This Code is working for me but I don't know how to update existing data?

# forms.py
from django import forms
from authtools.forms import UserCreationForm
from betterforms.multiform import MultiModelForm
from .models import UserProfile

class UserProfileForm(forms.ModelForm):
   
class Meta:
       
fields = ('favorite_color',)

class UserCreationMultiForm(MultiModelForm):
   
form_classes = {
       
'user': UserCreationForm,
       
'profile': UserProfileForm,
   
}

# views.py
from django.views.generic import CreateView
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import redirect
from .forms import UserCreationMultiForm

class UserSignupView(CreateView):
   
form_class = UserCreationMultiForm
   
success_url = reverse_lazy('home')

   
def form_valid(self, form):
       
# Save the user first, because the profile needs a user before it
       
# can be saved.
       
user = form['user'].save()
       
profile = form['profile'].save(commit=False)
       
profile.user = user
       
profile.save()
       
return redirect(self.get_success_url())

Reply all
Reply to author
Forward
Message has been deleted
0 new messages