Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#17845 closed New feature (invalid)

Provide a way for model formsets to know how many records will be created

Reported by: mstevens@… Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This is a feature request - please provide an easy way for model formsets to know how many records will be created.

As an example, I'm trying to write a clean() method for an inlineformset that checks that the database will always contain at least one model row when saved. The information to do this doesn't seem to be available.

Change History (2)

comment:1 by Aymeric Augustin, 12 years ago

Resolution: invalid
Status: newclosed

Unfortunately, this isn't something you can know in clean().

The sequence is as follows:

  • 1) clean(), well, cleans data received from the browser,
  • 2) existing instances present in the form, if any, are loaded from the database,
  • 3) existing instances are saved with the modifications and new instances are created.

That's why it isn't possible to know how many records will be created until step 2. However, you could check len(form.new_objects) after saving.

comment:2 by Florian Apolloner, 12 years ago

One (untested) possibility is:

def clean(self):
  super(BlaForm, self).clean()
  c = 0
  for f in self.extra_forms:
    if f.has_changed() and not self._should_delete_form(f):
      c += 1

After that c should tell you how many objects will get created, see save_new_objects and save_existing_objects for more info.

Note: See TracTickets for help on using tickets.
Back to Top