Opened 16 years ago
Closed 5 years ago
#13060 closed Cleanup/optimization (fixed)
ManagementForm exception in case of bad prefix should be easier to understand
| Reported by: | Karel | Owned by: | Manav Agarwal |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Normal | Keywords: | formset, ValidationError, ManagementForm |
| Cc: | shawntherrien@… | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
If user adds multiple formsets with prefixes, and specifies the prefix incorrectly when binding the form and validating:
some_formset = SomeFormSet(request.POST, 'articles')
instead of:
some_formset = SomeFormSet(request.POST, prefix='articles')
Django "suppresses" the original exception and raises only relatively unhelpful "ManagementForm data is missing or has been tampered with".
In file django/forms/formsets.py, line 57:
54. if self.data or self.files:
55. form = ManagementForm(self.data, auto_id=self.auto_id, prefix=self.prefix)
56. if not form.is_valid():
57. raise ValidationError('ManagementForm data is missing or has been tampered with')
Suggestion: include form._errors in output, because for such a small bug in code, it can take a really long time find it.
{'INITIAL_FORMS': [u'This field is required.'],
'MAX_NUM_FORMS': [u'This field is required.'],
'TOTAL_FORMS': [u'This field is required.']}
Attachments (1)
Change History (16)
by , 16 years ago
| Attachment: | formsets.diff added |
|---|
comment:1 by , 16 years ago
| Cc: | added |
|---|---|
| Has patch: | set |
comment:2 by , 16 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
Not sure I completely agree with the suggested fix, but yes -- the error on failure of the management form could be a little more helpful, at the very least naming the field that is missing or invalid.
comment:3 by , 15 years ago
| Type: | → New feature |
|---|
comment:4 by , 15 years ago
| Severity: | → Normal |
|---|
comment:5 by , 15 years ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
| Type: | New feature → Cleanup/optimization |
Patch would need improvement as per Russell's comment. Also needs tests.
comment:8 by , 5 years ago
| Easy pickings: | set |
|---|---|
| Has patch: | unset |
| Needs tests: | unset |
| Patch needs improvement: | unset |
| Version: | → master |
comment:9 by , 5 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:10 by , 5 years ago
We may change the code from :
raise ValidationError(
_('ManagementForm data is missing or has been tampered with'),
code='missing_management_form',
)
to something like:
raise ValidationError(
_('ManagementForm data is missing or has been tampered with %s' % form._errors),
code='missing_management_form',
)
Approvals please.
comment:12 by , 5 years ago
| Needs tests: | unset |
|---|
On second thought I suppose it’s a bit early for the needs tests flag since no one has supplied a review.
comment:13 by , 5 years ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
comment:14 by , 5 years ago
| Needs tests: | unset |
|---|---|
| Patch needs improvement: | unset |
| Triage Stage: | Accepted → Ready for checkin |
validation error with _errors included