Opened 13 years ago

Closed 13 years ago

Last modified 12 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: Daniel Hepper
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 by jurgeni, 13 years ago

Type: UncategorizedBug

comment:2 by Daniel Hepper, 13 years ago

Owner: changed from nobody to Daniel Hepper

comment:3 by Daniel Hepper, 13 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Daniel Hepper, 13 years ago

Has patch: set

comment:5 by Daniel Hepper <daniel.hepper@…>, 13 years ago

Resolution: fixed
Status: newclosed

In [0ae727beda8b34deb5061d87fbbeb40e5ac80db3]:

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

comment:6 by Aymeric Augustin <aymeric.augustin@…>, 13 years ago

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 by Eduardo Cereto, 12 years ago

Cc: eduardocereto@… added

This fix also fixed #18660

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