Opened 2 years ago

Closed 2 years ago

#33894 closed Bug (duplicate)

Underscore in CharField as Primary Key breaks related link displayed by admin's ForeignRawIdWidget

Reported by: Robert-Lebedeu Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords: Underscore, CharField, Primary Key, link, reverse
Cc: lorenzo.morandini@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (2)

comment:1 by Robert-Lebedeu, 2 years ago

Version: 3.2dev

comment:2 by Mariusz Felisiak, 2 years ago

Has patch: unset
Resolution: duplicate
Status: newclosed
Summary: Bug: Underscore in CharField as Primary Key breaks related link displayed by admin's ForeignRawIdWidgetUnderscore in CharField as Primary Key breaks related link displayed by admin's ForeignRawIdWidget

Duplicate of #30386.

Note: See TracTickets for help on using tickets.
Back to Top