Hi,
On Friday, December 5, 2014 8:22:07 PM UTC-5, Brad Rice wrote:
-- This might be a complicated enough case where a functional view might help make things more clear what's going on.
Collin
On Friday, December 5, 2014 8:22:07 PM UTC-5, Brad Rice wrote:
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 modelclass 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 formclass ReferenceForm(forms.ModelForm): class Meta:model = Referenceexclude = ('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 = Referenceform_class = ReferenceFormtemplate_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 formand its inline formsets."""self.object = Noneform_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 withassociated Ingredients and Instructions and then redirects to asuccess page."""self.object = form.save()reference_form.instance = self.objectreference_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+unsubscribe@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/de28611b-d13f-4eca-b958-19b243640572%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment