Adding links to full change forms for inline items in django admin?

前端 未结 7 554
执念已碎
执念已碎 2021-01-31 02:36

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 corr

7条回答
  •  天涯浪人
    2021-01-31 03:09

    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'Edit', url)
            # … or if you want to include other fields:
            return format_html(u'Edit: {}', url, instance.title)
    
        readonly_fields = ('admin_link',)
    

提交回复
热议问题