For the life of me I can't figure out how to use a modelformset with class view. I get it to display but it won't save. Also, when it goes into form_invalid, it doesn't display the 3 formsets. Can anyone shed a little light?
I'm using crispy forms with this in my template: {% crispy reference_form referencehelper %} This is my model class Reference(models.Model): ref_name = models.CharField(max_length=256) ref_phone = models.CharField(max_length=20, validators=[RegexValidator(regex='\(?([0-9]{3})\)?([ .-]?)([0-9]{3})([ .-]?)([0-9]{4})', message='Looks like your phone number is not formatted correctly', code='Invalid number')]) ref_email = models.EmailField(max_length=256) created_by = models.ForeignKey(User, unique=True) applicant = models.ForeignKey("Applicant") application = models.ForeignKey("Application") This is my form.py form class ReferenceForm(forms.ModelForm): class Meta: model = Reference exclude = ('created_by', 'application', 'applicant') def __init__(self, *args, **kwargs): super(ReferenceForm, self).__init__(*args, **kwargs) class ReferenceFormSetHelper(FormHelper): def __init__(self, *args, **kwargs): super(ReferenceFormSetHelper, self).__init__(*args, **kwargs) self.form_method = 'post' self.layout = Layout( Fieldset('Reference', 'ref_name', 'ref_phone', 'ref_email', )) self.add_input(Submit("submit", "Save")) ReferenceFormSet = modelformset_factory(Reference, form=ReferenceForm, extra=3, max_num=3, can_delete=True) This is my view: class ReferenceCreate(LoginRequiredMixin, CreateView): model = Reference form_class = ReferenceForm template_name = 'requestform/reference_form.html' def dispatch(self, *args, **kwargs): return super(ReferenceCreate, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): """ Handles GET requests and instantiates blank versions of the form and its inline formsets. """ self.object = None form_class = self.get_form_class() form = self.get_form(form_class) reference_form = ReferenceFormSet() refhelper = ReferenceFormSetHelper() return self.render_to_response( self.get_context_data( reference_form=reference_form, referencehelper=refhelper )) def form_valid(self, form): """ Called if all forms are valid. Creates a Recipe instance along with associated Ingredients and Instructions and then redirects to a success page. """ self.object = form.save() reference_form.instance = self.object reference_form.save() return HttpResponseRedirect(reverse('requestform:app_check', kwargs={'app_id': obj.application_id})) def form_invalid(self, reference_form): refhelper = ReferenceFormSetHelper() return self.render_to_response(self.get_context_data( reference_form=reference_form, referencehelper=refhelper )) -- 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 http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e823091d-a160-4575-b26e-56872aa21831%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.