Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#18433 closed Bug (fixed)

"View on site" does not work in InlineModelAdmin when using custom primary key field

Reported by: jurgeni Owned by: dhepper
Component: contrib.admin Version: 1.4
Severity: Normal Keywords: admin template
Cc: eduardocereto@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This bug appears when using custom primary key fields with different name than id, foreign key (or many to many field) relation between models, get_absolute_url defined for models and using InlineModelAdmin in Admin interface. For example the following case suffers this:

models.py

from django.db import models

class Model1(models.Model):
    my_own_pk = models.CharField(max_length=100, primary_key=True)
    name = models.CharField(max_length=100)

class Model2(models.Model):
    my_own_pk = models.CharField(max_length=100, primary_key=True)
    name = models.CharField(max_length=100)
    other_model = models.ForeignKey(Model1)

    def get_absolute_url(self):
        return '/model2/'

admin.py

from django.contrib import admin
from demo.models import Model1, Model2

class Model2Inline(admin.TabularInline):
    model = Model2
    extra = 1

class Model1Admin(admin.ModelAdmin):
    inlines = (Model2Inline,) 


admin.site.register(Model1, Model1Admin)
admin.site.register(Model2)

Now the "View on site" for InlineModel Model2 in Model1 in admin interface does not work because the template uses field id for creating link instead of pk. In my case the link uri is http://localhost:8000/admin/r/14// that redirects nowhere. If the template is changed to use pk instead of id the link uri is http://localhost:8000/admin/r/14/<the value of the pk here>/ and redirect works. The bug is in both TabularInline and StackedInline templates.

django/contrib/admin/templates/admin/edit_inline/stacked.html line 9:

{% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}

Should be:

{% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.pk }}/">{% trans "View on site" %}</a>{% endif %}

django/contrib/admin/templates/admin/edit_inline/tabular.html line 30:

{% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}

Should be:

{% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.pk }}/">{% trans "View on site" %}</a>{% endif %}

Change History (7)

comment:1 Changed 3 years ago by jurgeni

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

comment:2 Changed 3 years ago by dhepper

  • Owner changed from nobody to dhepper

comment:3 Changed 3 years ago by dhepper

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 3 years ago by dhepper

  • Has patch set

comment:5 Changed 3 years ago by Daniel Hepper <daniel.hepper@…>

  • Resolution set to fixed
  • Status changed from new to closed

In [0ae727beda8b34deb5061d87fbbeb40e5ac80db3]:

Fixed #18433 -- Fixed "View on Site" link in inline admin for models with custom PK

comment:6 Changed 3 years ago by Aymeric Augustin <aymeric.augustin@…>

In [c1729510aae7eb9e534ef62dfd54c37a6f89c193]:

Merge pull request #127 from dhepper/master

Fixed #18433 -- Fixed "View on Site" link in inline admin for models with custom PK

Thanks dhepper for the patch and apollo13 for the review.

comment:7 Changed 3 years ago by eduardocereto

  • Cc eduardocereto@… added

This fix also fixed #18660

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