--
Ticket URL: <https://code.djangoproject.com/ticket/17034>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_docs: => 0
* stage: Unreviewed => Design decision needed
* needs_tests: => 0
* needs_better_patch: => 0
Comment:
I could verify this issue. As far as the admin is concerned, this is
happening in the `get_object()` method:
https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py?rev=16934#L475
In that method, the object_id is initially prepared by doing:
{{#!python
object_id = model._meta.pk.to_python(object_id)
}}}
In the case of a "normal" model (without inheritance), the pk field is an
`AutoField` and therefore the line above raises a `ValidationError`. Then,
`get_object()` catches the error, ignores it, and moves on peacefully. So
eventually a 404 page is returned.
However, in the case of a "complex" model (using inheritance), the pk is a
`OneToOneField`, whose `to_python()` method simply returns the value
('blahblah') unchanged without raising any exception. That value is then
used in `queryset.get(pk=object_id)`, which raises a `ValueError`, which
isn't caught by `get_object()`.
The question is, should a `ValidationError` be returned by `OneToOneField`
(or any `ForeignKey`) when a value with unexpected format is provided to
`to_python()`, or should the admin's `get_object()` silently catch and
ignore `ValueError`?
--
Ticket URL: <https://code.djangoproject.com/ticket/17034#comment:1>
* cc: vlastimil.zima@… (added)
Comment:
I suggest to fix `to_python()` calls on relation fields. They should
validate data by the rules of related field. Otherwise similar bug
(`ValueError` instead of `ValidationError`) can occur in different places.
--
Ticket URL: <https://code.djangoproject.com/ticket/17034#comment:2>
* stage: Design decision needed => Accepted
Comment:
This should raise a 404, not a 500.
--
Ticket URL: <https://code.djangoproject.com/ticket/17034#comment:3>
* cc: chris.jerdonek@… (added)
Comment:
I ran into a similar issue where passing a non-integer string into
`ForeignKey.validate()` when the corresponding type should be an `int`
raised a `ValueError` instead of a `ValidationError`, which caused calling
code to break unexpectedly. The stack trace passes through
[https://github.com/django/django/blob/9ae17d994b1b0dee952c2e64f3727ce7a6ffb398/django/db/models/fields/related.py#L1345
this line]:
{{{
qs = self.rel.to._default_manager.using(using).filter(
**{self.rel.field_name: value}
)
}}}
Is this issue a different issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/17034#comment:4>
* status: new => closed
* resolution: => duplicate
Comment:
Original description reported again and fixed by #19951. Issue from
comment 4 looks like a different problem, but is hard to
duplicate/determine if still a problem without more complete details about
how to recreate, and full traceback. Closing since the original issue has
been fixed; if the line of code mentioned in comment 4 is really an error
that needs its own ticket with more complete details of the problem there.
--
Ticket URL: <https://code.djangoproject.com/ticket/17034#comment:5>