[Answered ]-How to put a hidden fields in a formset and instantiate this value?

2👍

Exclude the user from the list of fields,

ReplyFormSet = modelformset_factory(model=Reply, fields=('question', 'answer'), extra=length_questions, can_delete=True)

then set it when you are saving the formset.

if request.method == 'POST':  
    formset = ReplyFormSet(request.POST, queryset=Reply.objects.none())
    if formset.is_valid():
        new_instances = formset.save(commit=False)
        for new_instance in new_instances:
            new_instance.user = logged_user
            new_instance.save()

Leave a comment