On Sun, Apr 22, 2012 at 5:44 PM, Martin Tiršel <martin.tir...@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 post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to