Wednesday, April 25, 2012

Re: Ignoring empty forms in a formset

I am using class based views and my code is:

class PayOrdersView(AdminFormSetView):
    form_class = PayOrderForm
    template_name = 'internal/orders/pay_orders_form.html'
    active_item = 'order_pay_orders'
    context_formset_name = 'pay_orders_formset'
    extra = 2

    def formset_valid(self, formset):
        logger.debug('Executing formset_valid')
        for form in formset:
            logger.debug('Is empty: %s' % form.empty_permitted)
            form.save()
        return super(PayOrdersView, self).formset_valid(formset)


formset_valid method is called after formset.is_valid(). I start with 2 empty forms, I insert into first form order number and the second form stays empty. After I submit, I get:

[2012-04-25 13:42:07,776] DEBUG [31099 140249342375680] [project.internal.mixins:304] Processing POSTed form
[2012-04-25 13:42:07,778] DEBUG [31099 140249342375680] [project.internal.forms:29] Cleaning order_number
[2012-04-25 13:42:07,837] DEBUG [31099 140249342375680] [project.internal.mixins:307] Formset is valid
[2012-04-25 13:42:07,842] DEBUG [31099 140249342375680] [project.internal.views:93] Executing formset_valid
[2012-04-25 13:42:07,843] DEBUG [31099 140249342375680] [project.internal.views:95] Is empty: True
[2012-04-25 13:42:07,843] DEBUG [31099 140249342375680] [project.internal.forms:54] Saving PayOrderForm
[2012-04-25 13:42:09,914] DEBUG [31099 140249342375680] [project.internal.views:95] Is empty: True
[2012-04-25 13:42:09,914] DEBUG [31099 140249342375680] [project.internal.forms:54] Saving PayOrderForm

So both forms have empty_permitted == True. Management form in time of submit looks so:

<input id="id_form-TOTAL_FORMS" type="hidden" value="2" name="form-TOTAL_FORMS">
<input id="id_form-INITIAL_FORMS" type="hidden" value="0" name="form-INITIAL_FORMS">
<input id="id_form-MAX_NUM_FORMS" type="hidden" name="form-MAX_NUM_FORMS">

Thanks, Martin

On Wednesday, April 25, 2012 11:03:49 AM UTC+2, Tom Evans wrote:
On Sun, Apr 22, 2012 at 5:44 PM, Martin Tiršel <martin.tirsel@gmail.com> wrote:
> Hello,
>
> I have a formset and some JavaScript to add more forms into this formset. In
> a view, I iterate through the formset saving (not a ModelForm just Form with
> save method) each form:
>
> for form in formset:
>     form.save()
>
> But I want to ignore empty forms that were added by JavasScript and not
> removed. How can I do this?
>
> Thanks,
> Martin
>

You don't show much of your code, but I presume you have called
formset.is_valid() at this point?

If so, this pattern is pretty canonical:

if formset.is_valid():
    for form in formset:
        if form.is_valid() and not form.empty_permitted:
            form.save()

Extra forms in a formset are all instantiated with empty_permitted=True.

There are other things to be aware of though. This logic will not take
into account deleted forms etc, which is why there is a
BaseModelFormSet with the right behaviour baked into it's save()
method.

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/aKliGdMNqGwJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment