[Django] #33894: Bug: Underscore in CharField as Primary Key breaks related link displayed by admin's ForeignRawIdWidget

5 views
Skip to first unread message

Django

unread,
Aug 4, 2022, 10:10:03 AM8/4/22
to django-...@googlegroups.com
#33894: Bug: Underscore in CharField as Primary Key breaks related link displayed
by admin's ForeignRawIdWidget
-------------------------------------+-------------------------------------
Reporter: Robert- | Owner: nobody
Lebedeu |
Type: Bug | Status: new
Component: | Version: 3.2
contrib.admin | Keywords: Underscore,
Severity: Normal | CharField, Primary Key, link,
Triage Stage: | reverse
Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I understand that in order to handle primary keys that may contain special
characters, the usage of {{{django.contrib.admin.utils.quote}}} is
needed.

I see that the method {{{label_and_url_for_value(...)}}} of
{{{ForeignKeyRawIdWidget}}} doesn't escape the primary key when
calculating the related object URL.

I suppose the solution to this problem would be adding
{{{args=quote(obj.pk),}}} to the {{{reverse}}} call.

Small example to re-create the BUG:
1. Create following models:
{{{
from django.db import models

class ModelA(models.Model):
id = models.CharField(max_length=100, primary_key=True)

class ModelB(models.Model):
a_model = models.ForeignKey(ModelA)
}}}
2. Enable and register both models to the admin
3. Add {{{a_model}}} field to {{{raw_id_fields}}} of ModelBAdmin
4. Create one instance per model
5. Try using related link displayed by {{{ForeignKeyRawIdWidget}}} and see
how it doesn't work.

--
Ticket URL: <https://code.djangoproject.com/ticket/33894>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 4, 2022, 10:21:37 AM8/4/22
to django-...@googlegroups.com
#33894: Bug: Underscore in CharField as Primary Key breaks related link displayed
by admin's ForeignRawIdWidget
-------------------------------------+-------------------------------------
Reporter: Robert-Lebedeu | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:
Keywords: Underscore, | Triage Stage:
CharField, Primary Key, link, | Unreviewed
reverse |

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Robert-Lebedeu):

* version: 3.2 => dev


--
Ticket URL: <https://code.djangoproject.com/ticket/33894#comment:1>

Reply all
Reply to author
Forward
0 new messages