How to loop over a queryset for instantiate all formset?

I wish I could make a loop that would allow me to instantiate all my formsets for now I have written

def access(request, page_id):
    if request.method == 'POST':  
            formset = ReplyFormSet(request.POST, request.FILES, initial=[{'instance':instance,}])
    if formset.is_valid():
        #...
        return ...
    else:
        formset = ReplyFormSet(queryset=Reply.objects.filter(question=questions))
        return ...

On this last line that I recovered the information in the db but just for my first forms! Is there way to make a loop so that it instantiates me all the forms?

I tried something like this but it does not work :

else:
    for form in formset:
    form.formset = ReplyFormSet(queryset=Reply.objects.filter(question=questions))
return ...

This line only works for my first form :

formset = ReplyFormSet(queryset=Reply.objects.filter(question=questions))

How can I do to affect it at all?

Should I change my init function of my ReplyForm ?

Upvotes: 0

Views: 283

Answers (1)

doru
doru

Reputation: 9110

You could try:

formset = ReplyFormSet(queryset=Reply.objects.filter(question__in=questions))

Using the in filter you get every question which is in questions queryset.

Upvotes: 1

Related Questions