Get non-deleted forms from Django formset
Asked Answered
M

1

7

When I use a formset that's both can_order and can_delete I'm able to get all the deleted forms with .deleted_forms and all the non-deleted forms (in order) with .ordered_forms.

If I use a formset that doesn't use can_order I no longer have access to .ordered_forms (it throws an exception). Is there an easy way to get the list of forms that are not deleted?

The closest I can find is manually creating it with:

[form for form in formset.forms if not formset._should_delete_form(form)]

However, that's using a "hidden" method and it seems odd there's not something already built-in.

Magbie answered 10/5, 2018 at 21:29 Comment(0)
B
0

I'm using this to avoid using a "private" method:

[form for form in formset.forms if form not in formset.deleted_forms]
Bosley answered 14/4, 2022 at 12:11 Comment(1)
It's been 4 years, but I think i was specifically looking for some formset property that would be there regardless of can_order or can_delete. This works, but only if you always know can_delete=True. I guess you could test can_delete and if it were false then use formset.forms and if true then use what you've suggested.Magbie

© 2022 - 2024 — McMap. All rights reserved.