#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)
Change History (36)
by , 17 years ago
| Attachment: | formsets_min_num.diff added |
|---|
comment:1 by , 17 years ago
| Triage Stage: | Unreviewed → Design decision needed |
|---|
follow-up: 3 comment:2 by , 15 years ago
comment:3 by , 15 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 , 15 years ago
| Cc: | 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 , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → New feature |
comment:6 by , 14 years ago
| Cc: | added |
|---|---|
| Easy pickings: | unset |
| UI/UX: | unset |
comment:7 by , 14 years ago
| Easy pickings: | set |
|---|
by , 14 years ago
| Attachment: | 9532.patch added |
|---|
Added min_num argument to formset_factory, updated docs
comment:8 by , 13 years ago
| Cc: | added |
|---|
comment:9 by , 13 years ago
| Has patch: | set |
|---|
comment:10 by , 13 years ago
| Triage Stage: | Design decision needed → Accepted |
|---|
#17642 has a patch to apply this feature to admin inlines.
comment:11 by , 13 years ago
Hi, probably it is a good idea to add min_num to modelformset_factory and inlineformset_factory as well.
comment:12 by , 13 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:13 by , 13 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.
comment:15 by , 13 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 , 13 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:17 by , 13 years ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:18 by , 13 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:19 by , 12 years ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:21 by , 12 years ago
| Cc: | added |
|---|---|
| Needs documentation: | set |
| Patch needs improvement: | set |
| Summary: | min_num on formsets → Add 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 , 12 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 , 12 years ago
| Cc: | added |
|---|---|
| Summary: | Add min_num on formsets → Add 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 , 12 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 , 12 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 , 12 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
comment:27 by , 12 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 , 12 years ago
| Cc: | 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 , 12 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 , 12 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 , 12 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 , 12 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.
double +1. This was very hard for me to work around, and would be a very logical addition.