I think This is very easy anyone 1 Year experienced guy can answer it.
On Monday, June 25, 2018 at 6:51:14 PM UTC+5:30, Sunil Kothiyal wrote:
>
> 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())
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/94c3bcc9-3f89-47da-869f-de031f77f5d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.