46
votes

I have a standard admin change form for an object, with the usual StackedInline forms for a ForeignKey relationship. I would like to be able to link each inline item to its corresponding full-sized change form, as the inline item has inlined items of its own, and I can't nest them.

I've tried everything from custom widgets to custom templates, and can't make anything work. So far, the "solutions" I've seen in the form of snippets just plain don't seem to work for inlines. I'm getting ready to try some DOM hacking with jQuery just to get it working and move on.

I hope I must be missing something very simple, as this seems like such a simple task!

Using Django 1.2.

7
I found the answer given in stackoverflow.com/questions/2120813 better.Florian Ledermann
Django >= 1.8, use show_change_link stackoverflow.com/a/28170958/3218806maxbellec

7 Answers

124
votes

There is a property called show_change_link since Django 1.8.

47
votes

I did something like the following in my admin.py:

from django.utils.html import format_html
from django.core.urlresolvers import reverse

class MyModelInline(admin.TabularInline):
    model = MyModel

    def admin_link(self, instance):
        url = reverse('admin:%s_%s_change' % (instance._meta.app_label,  
                                              instance._meta.module_name),
                      args=(instance.id,))
        return format_html(u'<a href="{}">Edit</a>', url)
        # … or if you want to include other fields:
        return format_html(u'<a href="{}">Edit: {}</a>', url, instance.title)

    readonly_fields = ('admin_link',)
18
votes

The currently accepted solution here is good work, but it's out of date.

Since Django 1.3, there is a built-in property called show_change_link = True that addresses this issue.

This can be added to any StackedInline or TabularInline object. For example:

class ContactListInline(admin.TabularInline):
    model = ContactList
    fields = ('name', 'description', 'total_contacts',)
    readonly_fields = ('name', 'description', 'total_contacts',)
    show_change_link = True

The result will be something line this:

tabular inline using show_change_link

11
votes

I had similar problem and I came up with custom widget plus some tweaks to model form. Here is the widget:

from django.utils.safestring import  mark_safe    

class ModelLinkWidget(forms.Widget):
    def __init__(self, obj, attrs=None):
        self.object = obj
        super(ModelLinkWidget, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        if self.object.pk:
            return mark_safe(
                u'<a target="_blank" href="../../../%s/%s/%s/">%s</a>' %\
                      (
                       self.object._meta.app_label,
                       self.object._meta.object_name.lower(),
                       self.object.pk, self.object
                       )
            )
        else:
            return mark_safe(u'')

Now since widget for each inline need to get different object in constructor you can't just set it in standard way, but in Form's init method:

class TheForm(forms.ModelForm):
    ...
    # required=False is essential cause we don't
    # render input tag so there will be no value submitted.
    link = forms.CharField(label='link', required=False)

    def __init__(self, *args, **kwargs):
        super(TheForm, self).__init__(*args, **kwargs)
        # instance is always available, it just does or doesn't have pk.
        self.fields['link'].widget = ModelLinkWidget(self.instance)
3
votes

Quentin's answer above works, but you also need to specify fields = ('admin_link',)

2
votes

There is a module for this purpose. Check out: django-relatives

0
votes

I think: args=[instance.id] should be args=[instance.pk]. It worked for me!