Ben
Ben

Reputation: 16710

Inline Formset Object Not Iterable

My ultimate goal here is to save Action modelforms for a given website (the foreign key). After form validation, I then want to sum the points for all the individual Actions and confirm that it's below a certain threshold (100 points) before I save the Actions. If the total exceeds 100, I'll raise a ValidationError.

My issue here is that I'm receiving the following error message:

"'ActionFormFormSet' object is not iterable"

The instances exist, so the issue seems to be iterating over this particular object. In the official documentation, there's an example that iterates over a modelformset in this exact fashion. However, the modelformset is populated by a queryset, whereas the inlineformset is not explicitly populated in the same way(maybe implicitly, I don't know).

Can I just not iterate over this object? What should I do here?

Thanks

 ActionFormSet=inlineformset_factory(Website, Action, extra=1, can_delete=True)
 if request.method=='POST':
     action_formset=ActionFormSet(request.POST, instance=website,prefix="actions")
     if action_formset.is_valid():

        #After validating the surveys, I need to make sure total points<100
        for form in action_formset:
            pass
        action_formset.save()

Upvotes: 0

Views: 1865

Answers (2)

prauchfuss
prauchfuss

Reputation: 1970

You might be using an older version of django. The formsets are only iterable in 1.3+ I believe. This might work:

for form in action_formset.forms:
    pass
action_formset.save()

Upvotes: 2

maximus
maximus

Reputation: 161

First save the formset then iterate over the objects

forms = action_formset.save( commit = False)

now iterate over the forms by:

for form in forms:
    # do something

Upvotes: 0

Related Questions