#36201: ModelChoiceField/ModelMultipleChoiceField.clean() should catch
ValidationError raised by the queryset operations
-------------------------------------+-------------------------------------
     Reporter:  Tim Graham           |                     Type:
                                     |  Cleanup/optimization
       Status:  new                  |                Component:  Forms
      Version:  dev                  |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 As seen in cdc25ac4747bf5a6cdc2e70461c2d43c54529d35, `ValueError` and
 `TypeError` are caught by the queryset operations in
 `ModelChoiceField.clean()` and `ModelMultipleChoiceField.clean()`,
 however, these exceptions are particular to
 
[https://github.com/django/django/blob/65c46d6932c0956d2988d13ec3d9ab3ef9d96d61/django/db/models/fields/__init__.py#L2127-L2132
 IntegerFielld.get_prep_value()]. A more common implementation of
 `get_prep_value()` delegates to `to_python()` which is supposed to raise
 `ValidationError` for invalid values
 
([https://github.com/django/django/blob/65c46d6932c0956d2988d13ec3d9ab3ef9d96d61/django/db/models/fields/__init__.py#L2748-L2773
 as seen in UUIDField]). Thus, `ValidationError` should also be caught so
 that the
 
[https://github.com/django/django/blob/65c46d6932c0956d2988d13ec3d9ab3ef9d96d61/django/forms/models.py#L1566-L1570
 "invalid choice"] error  is raised rather than the "invalid" error of the
 model field.

 Aside: I discovered this while implementing `ObjectIdAutoField` in django-
 mongodb-backend. I had some difficulty figuring out what exception
 `get_prep_value()` should raise. I have to do some more digging to
 understand if `IntegerFielld.get_prep_value()` raising `ValueError` and
 `TypeError` is really appropriate, but this is the subject of another
 ticket.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36201>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701952158dc7e-e2163fad-8204-484d-8aad-0085ac33fbea-000000%40eu-central-1.amazonses.com.

Reply via email to