StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 8 · ModelForms & Formsets

Bulk Ticket Formset

mediumDjango FormsetsDatabase Transactions100%2 of 2 solved

Problem statement

In this Django project, implement group registration as one submission instead of one attendee at a time. Build the AttendeeFormSet class and the attendee_group view in app/views.py so the whole group is validated together and written inside one transaction. This stops a typo or a full room from leaving half a group registered and the other half lost.

Requirements

Implement:

  • AttendeeFormSet in app/views.py
  • attendee_group in app/views.py

The following files are already implemented and provided: app/models.py, app/forms.py, app/urls.py and the template. Attendee holds name (CharField(max_length=60)) and email (EmailField) and is ordered by name. AttendeeForm is the ModelForm for one row, those two fields and their labels. app/models.py also holds CAPACITY = 6 and the exception SeatsSoldOut, which Attendee.save() raises when writing one more attendee would take the room past CAPACITY. app/templates/app/attendees.html renders formset from the context: the management form first, then one block per form in it, each with its hidden id, its two fields, a delete box on every row that is already saved, and its own error area.

1. AttendeeFormSet - the formset class

  • Built from AttendeeForm with modelformset_factory over Attendee.
  • The sheet offers exactly three blank rows on top of the attendees already registered: a blank page declares three rows, a page over two saved attendees declares five, two of them existing.
  • Every saved row can be marked for deletion.
  • Do not set max_num: the room's capacity is the database's business, not the page's.

2. GET /attendees/

  • Purpose: Display the group sheet.
  • Response:
  • 200 OK: the template rendered with the formset published in the context as formset, unbound, over the registered attendees, every row built from the given AttendeeForm.

3. POST /attendees/

  • Purpose: Register or amend the whole group in one submission.
  • Validation: the batch is asked once whether it is valid. Do not loop over the forms calling is_valid() or save() one row at a time to decide what to keep.
  • Response:
  • 302 Found: redirect to the route named attendee-group when every row is valid, with the batch written - rows ticked for deletion removed, existing rows updated, filled blank rows created.
  • 200 OK: the sheet re-rendered with the submitted values still in the inputs and each error on the row that earned it, with nothing saved at all: not the invalid row, and not the valid rows on either side of it.
  • A submission whose management form is missing or carries a nonsense row count is invalid as a whole and writes nothing, rather than being half read.

4. All or nothing

  • The whole batch is written inside one transaction.atomic() block, so when the room fills up half way through a group, SeatsSoldOut comes out of the view and the database holds exactly what it held before the submission.
  • Let formset.save() do the writing, or do exactly what it does, deletions included, inside the atomic block.

Notes

  • Do not catch SeatsSoldOut. The requirement is that nothing is left behind when it is raised, not that the visitor is spared the error.
  • The capacity check cannot be made before the batch starts, because how full the room is depends on the rows written just before.
  • Build the redirect from the route name rather than writing the path as a string.
  • Do not change the template, the form, the model or the URL map, and do not add a field to AttendeeForm.

More in ModelForms & Formsets

See the module
Product ModelFormA product form restates every model field by hand, so it drifts from the model and the model's own rules never reach it. Derive the form from the model with Meta, give the declared fields their widgets and labels, and save through the form with commit=False so the view can stamp the server-set listing code.100%2 of 2 solvedmediumUnique Slug ValidationA duplicate slug reaches the database instead of coming back as a form error, and editing a page false-positives against its own slug. Layer the slug rules and a self-excluding uniqueness check on the ModelForm, with a unique constraint on the model as the backstop.100%2 of 2 solvedmediumInvoice With Line ItemsAn invoice is a header plus a growable table of lines, and a single form cannot take both or keep them consistent. Build the editor from a header ModelForm and an inline item formset bound to the parent, layer field, form and cross-formset rules over it, and save parent and children together inside one transaction.100%2 of 2 solvedmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.