#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 , 13 years ago
Type: | Uncategorized → Bug |
---|
comment:2 by , 13 years ago
Owner: | changed from | to
---|
comment:3 by , 13 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 13 years ago
Has patch: | set |
---|
comment:5 by , 13 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Patch available on GitHub: https://github.com/django/django/pull/127