﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18433	"""View on site"" does not work in InlineModelAdmin when using custom primary key field"	jurgeni	Daniel Hepper	"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 %}
}}}"	Bug	closed	contrib.admin	1.4	Normal	fixed	admin template	eduardocereto@…	Accepted	1	0	0	0	0	0
