Opened 2 months ago
Last modified 7 weeks ago
#36482 assigned Bug
Admin renders complex PKs incorrectly
Reported by: | David Sanders | Owned by: | David Sanders |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Admin creates links to edit/history/etc pages by rendering the PK directly as a string - either explicitly in Python or implicitly in one of the templates.
However when the instance is retrieved in get_object()
it uses field.to_python(value)
like so: https://github.com/django/django/blob/a3882876927a819ca982ea61353945e9d86f3659/django/contrib/admin/options.py#L886
This creates an asymmetrical serialisation-deserialisation process which, while is fine for integers, will fail for things more complex.
The correct serialisation of pk would be the inverse of to_python()
: field.value_to_string(obj)
.
Note this is related to #35953: The fix for this would also be useful to get composite PKs working - at least for rendering the PK.
I noticed this while trying out DateTimeRangeField()
as part of the PK for temporal models as the valid_time
– this field type uses specific json serialisation which is quite verbose in the URL but works (have tested it will post proposed patch in a draft PR)
Change History (4)
comment:1 by , 2 months ago
comment:2 by , 2 months ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 2 months ago
Has patch: | set |
---|---|
Owner: | set to |
Status: | new → assigned |
I think I've got something worth reviewing now 👍
comment:4 by , 7 weeks ago
Needs tests: | set |
---|
Draft PR: https://github.com/django/django/pull/19600