Opened 16 years ago

Closed 11 years ago

Last modified 11 years ago

#9532 closed New feature (fixed)

Add min_num and validate_min on formsets

Reported by: Gabriel Farrell Owned by: Rogério Yokomizo
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: mathijs@…, tgecho, kitsunde@…, timograham@…, me@…, tchristiansen Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description

Formsets have a max_num for limiting the number of forms, but no way to ensure a minimum.

Use case: I want an address formset that displays all addresses that have been created and no extras, unless there are no addresses, then one empty form should be displayed. I would set extra=0 and min_num=1.

Patch included is just a beginning. I may have missed a function or two and I haven't touched docs or tests.

Attachments (3)

formsets_min_num.diff (3.1 KB ) - added by Gabriel Farrell 16 years ago.
9532.patch (11.7 KB ) - added by michal@… 13 years ago.
Added min_num argument to formset_factory, updated docs
9532_2.patch (11.7 KB ) - added by michal@… 13 years ago.
Removed maxDiff = None from test case

Download all attachments as: .zip

Change History (36)

by Gabriel Farrell, 16 years ago

Attachment: formsets_min_num.diff added

comment:1 by Jacob, 16 years ago

Triage Stage: UnreviewedDesign decision needed

comment:2 by charliebubblegum, 14 years ago

double +1. This was very hard for me to work around, and would be a very logical addition.

in reply to:  2 comment:3 by anonymous, 14 years ago

Replying to charliebubblegum:

double +1. This was very hard for me to work around, and would be a very logical addition.

agreed. will like to hear inputs from devs.

comment:4 by Mathijs de Bruin, 14 years ago

Cc: mathijs@… added

Bump! It would make a lot of sense to have this kind of functionality built into Django, and the effort would not be gigantic.

comment:5 by Luke Plant, 14 years ago

Severity: Normal
Type: New feature

comment:6 by tgecho, 13 years ago

Cc: tgecho added
Easy pickings: unset
UI/UX: unset

comment:7 by ted.tieken@…, 13 years ago

Easy pickings: set

by michal@…, 13 years ago

Attachment: 9532.patch added

Added min_num argument to formset_factory, updated docs

by michal@…, 13 years ago

Attachment: 9532_2.patch added

Removed maxDiff = None from test case

comment:8 by Kit Sunde, 12 years ago

Cc: kitsunde@… added

comment:9 by Thejaswi Puthraya, 12 years ago

Has patch: set

comment:10 by Claude Paroz, 12 years ago

Triage Stage: Design decision neededAccepted

#17642 has a patch to apply this feature to admin inlines.

comment:11 by wim@…, 12 years ago

Hi, probably it is a good idea to add min_num to modelformset_factory and inlineformset_factory as well.

comment:12 by AnkitBagaria, 12 years ago

Owner: changed from nobody to AnkitBagaria
Status: newassigned

comment:13 by Till Backhaus, 12 years ago

Hi AnkitBagria, I just started working on this too. I'm in Utrecht right now, I'm also in #django-sprint. My nick is tback. Maybe we can work on this together.

Last edited 12 years ago by Till Backhaus (previous) (diff)

comment:15 by Till Backhaus, 12 years ago

Sorry, it's not fixed yet. BaseModelFormsets are still broken. The current state of my work is here: https://github.com/tback/django/tree/formset-min-num

comment:16 by AnkitBagaria, 12 years ago

Owner: AnkitBagaria removed
Status: assignednew

comment:17 by AnkitBagaria, 12 years ago

Owner: set to AnkitBagaria
Status: newassigned

comment:18 by AnkitBagaria, 12 years ago

Owner: AnkitBagaria removed
Status: assignednew

comment:19 by Rogério Yokomizo, 11 years ago

Owner: set to Rogério Yokomizo
Status: newassigned

comment:20 by Rogério Yokomizo, 11 years ago

comment:21 by Tim Graham, 11 years ago

Cc: timograham@… added
Needs documentation: set
Patch needs improvement: set
Summary: min_num on formsetsAdd min_num on formsets

Thanks, the patch no longer merges cleanly so it needs to be rebased. It also needs documentation including a mention in the 1.7 release notes. If you could add that, I will try to review and commit it.

comment:22 by Marc Tamlyn, 11 years ago

I'm pretty concerned about what the implementation for this looks like. It seems to me that every formset on every Django site would now have to have the extra "MIN" field. Whilst in theory this shouldn't break anyone's sites, it will almost certainly break everyone's tests. I'd prefer a less backwardsly problematic patch for a relatively minor feature.

That said, I think the feature is probably useful. Perhaps a patch could be devised which means the "MAX" field is not required either?

comment:23 by Rogério Yokomizo <me@…>, 11 years ago

Cc: me@… added
Summary: Add min_num on formsetsAdd min_num and validate_min on formsets

Patch rebased.

Could someone please review?

https://github.com/django/django/pull/1444

About backward problematic, I don't know how can I do that. Any suggestions?

comment:24 by Tim Graham, 11 years ago

Needs documentation: unset

I left comments on the pull request. I'm not sure there's actually any backwards compatibility concerns as the parameters aren't actually required in POST data as far as I can tell.

comment:25 by Rogério Yokomizo, 11 years ago

Thanks timo!

I adjust the pull request based on your comments.

I rebased it. The old commit with comments can be seen at https://github.com/yokomizor/django/commit/35d40939f89990c01f4c31ac10a29e3ec809d26e.

Could someone please review?

https://github.com/django/django/pull/1444

About the backward problematic, maybe the only backwards compatibility concerns are in tests that test formset.as_X() return.

comment:26 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In df27803a55ffea71d468a6aab0d897caa2b7a797:

Fixed #9532 -- Added min_num and validate_min on formsets.

Thanks gsf for the suggestion.

comment:27 by Tim Graham, 11 years ago

It would also be useful to add this to modelformset_factory, inlineformset_factory, etc. Ticket #17642 is open for that issue -- it has a patch and the branch from tback above also looks relevant.

comment:28 by tchristiansen, 11 years ago

Cc: tchristiansen added

Why is min_num added to extra?
This makes no sense to me, and makes extra useless in combination with min_num.

extra=3 and min_num=3 resulting 6 forms, which is absolutely not what I would expect (3 forms).

comment:29 by Rogério Yokomizo, 11 years ago

I think this is there because when you set min_num as 3 you want at least 3 extra forms.

Maybe we should remove it, or just clarify in the docs.

What about set extra equals to min_num when min_num is greater than extra?

comment:30 by Tim Graham, 11 years ago

I agree a documentation improvement would be welcome.

@tchristiansen, is there a case you foresee where min_num=X and you wouldn't want at least X extra forms?

comment:31 by Stephen Burrows, 11 years ago

@timo I would set min_num=3 if there should be at least three things. I would set extra=3 if I wanted three blank forms presented.

The implementation here means that there will *always* be six blank forms - existing objects are *not* taken into account.

To put it another way, if min_num is 3, and extra is 2, and there are already 2 existing objects, I would expect to have either 4 forms (2 extra on top of existing objects) or 5 forms (2 extra on top of the minimum.) With the code as it is, I will instead get seven forms - two for the existing objects, and an additional seven from extra.

Ran into this behavior while working on #17642. Possible this is only a modelformset issue.

comment:32 by Tim Graham, 11 years ago

I agree existing objects should be taken into account so the number of blank forms should be (min_num + extra - existing objects). Let's call it a bug and fix it now.

comment:33 by Stephen Burrows, 11 years ago

Opened a new ticket for this: #22628

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