Here is an example. I've cut out some of the code, but I this will give you the general idea. Note, I move the data from the Day form in to the Classes table. But you could just save both sets of data to their respective tables if desired.
in views.py class ClassesAddView(LoginRequiredMixin,View): classesFormClass = ClassesRegForm daysFormClass = DaysForm template_name = 'qing/classes.html' def get(self,request,role='NoRole'): classesForm = self.classesFormClass() context['form'] = classesForm daysForm = self.daysFormClass() context['daysform'] = daysForm return render(request,self.template_name, context) def post(self, request, *args, **kwargs): classesForm = self.classesFormClass(request.POST) daysForm = self.daysFormClass(request.POST) if classesForm.is_valid() and daysForm.is_valid(): days = str(request.POST.getlist('days')) reference = request.POST['reference'] classes = classesForm.save() classes = Classes.objects.get(reference=reference) classes.days = days classes.save() else: classesForm = self.classesFormClass(request.POST) context['form'] = classesForm daysForm = self.daysFormClass(request.POST) context['daysform'] = daysForm context['datavalid'] = False return render(request, self.template_name, context) return HttpResponseRedirect(reverse('classesadd',args=(role,))) in forms.py class ClassesForm(ModelForm): class Meta: model = Classes fields = ['reference','course','teachers',etc...] class DaysForm(forms.Form): OPTIONS = ( ("Sunday", "Sunday"), ("Monday", "Monday"), ("Tuesday", "Tuesday"), ("Wednesday", "Wednesday"), ("Thursday", "Thursday"), ("Friday", "Friday"), ("Saturday", "Saturday"), ) days = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=OPTIONS) On Friday, September 7, 2018 at 9:47:51 AM UTC-4, Akshay Gaur wrote: > > I think it would be easier to write out a custom form (without using your > model classes, just the fields that you will need for all the models) and > then in the save method for that form's view, you create model objects > using the fields in the POST request. > > On Friday, September 7, 2018 at 5:43:11 AM UTC-5, Django Lover wrote: >> >> >> I have one page, which I have to show three model form and three >> different submit button for each. >> >> My question is how I can save these three form individually? >> >> FOLLOWING IS CODE:- >> >> **form.py** >> >> >> class UserTaxesMultiForm(MultiModelForm): >> form_classes = { >> 'user_tax_form': MyForm, >> 'user_discount_form': DiscountForm, >> 'user_shiping_form': ShipmentForm, >> } >> >> *Note- myForm, DiscountForm, ShipmentForm are model forms. like >> following-* >> >> class MyForm(forms.ModelForm): >> prefix = 'tax' >> class Meta: >> model = StUserTaxDetails >> fields = ('tax_name', 'tax_rate') >> >> tax_name = forms.CharField(max_length=10, >> widget=forms.TextInput(), >> required=True, label="tax name") >> >> tax_rate = forms.FloatField(required=True, label="tax rate") >> >> >> error_messages = { >> 'required': _('fields are required.'), >> } >> >> def clean_title(self): >> return self.cleaned_data['tax_name'] >> >> def clean(self): >> tax_name = self.cleaned_data.get('tax_name') >> tax_rate = self.cleaned_data.get('tax_rate') >> >> if not tax_name and not tax_rate: >> raise forms.ValidationError( >> self.error_messages['required'], >> code='required', >> ) >> return self.cleaned_data >> >> **view.py** >> class AddTaxView(LoginRequiredMixin, CreateView): >> template_name = 'invoices/add_tax.html' >> form_class = UserTaxesMultiForm >> success_url = '/add/tax/' >> >> *{WHAT IS THE CODE HERE FOR THE POST METHOD TO SAVE DATA ACORDING >> DIFFRENT FORM SUBMIT} * >> >> >> >> >> **HTML** >> >> >> >> <form method="POST"> >> {% form.user_tax_form%} >> >> <input type="submit" value="Submit" /> >> </form> >> >> <form method="POST"> >> {% form.user_discount_form%} >> >> <input type="submit" value="Submit" /> >> </form> >> >> <form method="POST"> >> {% form.user_shiping_form%} >> >> <input type="submit" value="Submit" /> >> </form> >> >> PLEASE HELP >> > -- 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 django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. 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/122a47d7-5cdd-47ee-8524-fb55fb80285e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.