Bigair
Bigair

Reputation: 1592

How to remove a form from formset

views.py

formset = ComparisonScoreFormSet(initial=[
    {'comparison': comparison} for comparison in Comparison.objects.all()
])

I initialized a formset with initial values. This adds forms with initial values and also one form with no initial value.

I need to remove the form with no initial value.

I need to do something like the following

del formset[-1] #since the form with no initial value is at the last

Upvotes: 2

Views: 271

Answers (1)

CoffeeBasedLifeform
CoffeeBasedLifeform

Reputation: 2921

Try this:

formset = ComparisonScoreFormSet(
    initial=[{'comparison': comparison} for comparison in Comparison.objects.all()],
    extra = 0, # defines number of extra empty forms
)

Upvotes: 1

Related Questions