bodger
bodger

Reputation: 1182

Add form field to entire formset

I am using django-extra-views to create a formset for updating a bunch of model instances but I need to add an extra field to the entire formset - to ask the user whether they intend to email the relevant people about any changes to the formset or not.

Note that I am NOT intending to add a field to each form in the formset - just one extra field for the whole formset.

Any idea how would I go about that?

Upvotes: 3

Views: 1270

Answers (3)

Vojta Pasler
Vojta Pasler

Reputation: 11

You can add the field in the Template such as.

<form method="post">
    {% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset %}
     {{form}}
    {% endfor %}
    <input type="checkbox" name="send_email">
</form>

Upvotes: 1

Chris Brand
Chris Brand

Reputation: 11

You can use multiple forms in one template, multiple formsets in one template, and a combination of forms and formsets. You just have to provide different prefixes to distinguish them from one another.

So you can create a form with just one field (presumably a BooleanField) and then pass both your formset and the new form to the template.

The documentation doesn't describe exactly this, but comes pretty close: https://docs.djangoproject.com/en/2.1/ref/forms/api/#prefixes-for-forms https://docs.djangoproject.com/en/2.1/topics/forms/formsets/#using-more-than-one-formset-in-a-view

Upvotes: 1

Darshit
Darshit

Reputation: 430

I don't see any way to set field at formset, but you can work around. Use key in setting file which defines whether intimation should go for any changes in formset or not, and check as follow

if formset.has_changed() and settings.INTIMATION_KEY:
    send_mail()

Upvotes: 0

Related Questions