selectively remove forms from formset in view function

16 views
Skip to first unread message

Richard Brockie

unread,
May 19, 2016, 11:18:17 AM5/19/16
to Django users
Hi,

I'm working with pagination of a large formset in my view function. For forms outside the range to be viewed, I'm currently doing this:

for form in results_formset:
    place_to_test
= int(form['placing'].value())

   
if place_to_test not in visible_range:
        form
.fields['race_number'].widget = HiddenInput()


visible_range is a list of places that should be viewed according to the pagination.

However, most of the formset is hidden. Simply using a HiddenInput() results in a large number of non-visible forms still being sent from the server. I have a scheme where I will condense the hidden information from the non-visible forms to save bandwidth. To do this within the current code structure requires me to be able to remove a non-visible form from the formset.

My code should become something like:

for form in results_formset:

    place_to_test
= int(form['placing'].value())
    race_number
= form['race_number'].value()

   
if place_to_test not in visible_range:

       
# need method to remove form from results_formset
       
pass

       
# condensing the information to save bandwidth
       
if place_to_test < range_min:
            hidden_before
.append("%s:%s" % (place_to_test, race_number))
       
elif place_to_test > range_max:
            hidden_after
.append("%s:%s" % (place_to_test, race_number))


I can't find how to remove the non-visible form from the formset - can anyone provide a pointer?

Best wishes,
Richard Brockie

Richard Brockie

unread,
May 22, 2016, 7:53:58 PM5/22/16
to Django users
OK - after playing around with this I am finding that it is rather difficult. Instead, I recommend that you only create the forms that you need in the first place in the view function. This requires a change in the structure of my code, so I'll have to make do for the time being with the larger than I would like file size.
Reply all
Reply to author
Forward
0 new messages