Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33175 closed Bug (duplicate)

ForeignKeyRawIdWidget doesn't quote and generates wrong links.

Reported by: Alexander Pervakov Owned by: nobody
Component: contrib.admin Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When related model use CharField as primary key and it have some special characters, i.e. "_" then (in my case) on TabularInline admin view I've links without proper quoting that guides me to 302 redirect and error message "There is no product with id "some_id$"". Here the $ sign is a quoted _24 that you can see in an example:

# app/
class Product(models.Model):
    id = models.CharField(max_length=100, primary_key=True, null=False)

class Partner(models.Model):
    name = models.CharField(max_length=256)

class PartnerProductRelation(models.Model):
    partner = models.ForeignKey(
        Partner, related_name="product_links", on_delete=models.CASCADE,
    product = models.ForeignKey(
        Product, related_name="partner_links", on_delete=models.CASCADE,

# app/
class PartnerProductRelationAdmin(admin.TabularInline):
    model = PartnerProductRelation
    extra = 1
    raw_id_fields = ("product",)

class PartnerAdmin(admin.ModelAdmin):
    list_display = ("name",)
    fields = ("name",)
    inlines = [PartnerProductRelationAdmin], PartnerAdmin)

partner = Partner(id=1, name="Some Partner")
product = Product(id="some_id_24")
PartnerProductRelation(partner=partner, product=product)

url = reverse("admin:app_partner_change", args=(quote(,))
response = admin_client.get(url)
assert response.content.find(
    b"""<a href="/admin/app/product/some_5Fid_5F24/change/">some_id_24</a>"""
) > -1

Is tests/admin_widgets/ a good place to create the regression?

Also found not-quoted reverse that can lead to the same consequences here django.contrib.admin.options.ModelAdmin.response_change:

        elif "_saveasnew" in request.POST:
            redirect_url = reverse('admin:%s_%s_change' %
                                   (opts.app_label, opts.model_name),

Attachments (1)

admin_quote_object_pk.diff (828 bytes) - added by Alexander Pervakov 2 years ago.

Download all attachments as: .zip

Change History (3)

Changed 2 years ago by Alexander Pervakov

Attachment: admin_quote_object_pk.diff added

comment:1 Changed 2 years ago by Mariusz Felisiak

Component: Uncategorizedcontrib.admin
Resolution: duplicate
Status: newclosed
Type: UncategorizedBug

Duplicate of #30386.

comment:2 Changed 2 years ago by Mariusz Felisiak

Summary: ForeignKeyRawIdWidget doesn't use quoting for and generates unavailable links in adminForeignKeyRawIdWidget doesn't quote and generates wrong links.
Note: See TracTickets for help on using tickets.
Back to Top