Opened 11 years ago
Closed 11 years ago
#22808 closed Bug (fixed)
ModelMultipleChoiceField does not properly check if value is valid
| Reported by: | Owned by: | Niclas Olofsson | |
|---|---|---|---|
| Component: | Forms | Version: | 1.6 |
| Severity: | Normal | Keywords: | modelform, afraid-to-commit |
| Cc: | maxime.turcotte@… | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
ModelMultipleChoiceField does try to check if value is valid by doing a query here: https://github.com/django/django/blob/stable/1.6.x/django/forms/models.py#L1185
However, it only cathes ValueError, assuming that filter() is capable of even using the value.
If the value is a weird data type like list or dict, the call to filter throws TypeError.
ModelMultipleChoiceField.clean should catch TypeError, in addition to ValueError, on line 1187.
Here is gist with a complete minimal app that demonstrates how this can happen: https://gist.github.com/thnee/8e7c6b22f350582efe57/
Specifically the file: 4. views.py.
It is tested with 1.6.5, using 100% default settings in a clean test project.
Change History (9)
comment:1 by , 11 years ago
| Cc: | added |
|---|
comment:2 by , 11 years ago
| Easy pickings: | set |
|---|---|
| Version: | 1.5 → 1.6 |
comment:3 by , 11 years ago
| Keywords: | afraid-to-commit added |
|---|
comment:4 by , 11 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:6 by , 11 years ago
| Needs tests: | set |
|---|
It needs a test (or tests). Please uncheck "Needs tests" if you can update it, thanks.
comment:7 by , 11 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
Working on this during EP14 sprints.
comment:8 by , 11 years ago
| Has patch: | set |
|---|---|
| Needs tests: | unset |
New PR with the existing patch applied and including test cases.
https://github.com/django/django/pull/2964
comment:9 by , 11 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
Hi,
Looking at
django/forms/fields.py, it seems that there is precedent for catching(ValueError, TypeError)so I think it makes sense.The same error seems to be present in
ModelChoiceFieldso both should be fixed as part of this ticket.Thanks.