Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I add a link from the Django admin page of one object to the admin page of a related object?
    primarykey
    data
    text
    <p>To deal with the <a href="https://stackoverflow.com/questions/3681258/nested-inlines-in-the-django-admin">lack of nested inlines</a> in django-admin, I've put special cases into two of the templates to create links between the admin change pages and inline admins of two models.</p> <p><strong>My question is: how do I create a link from the admin change page or inline admin of one model to the admin change page or inline admin of a related model cleanly, without nasty hacks in the template?</strong></p> <p>I would like a general solution that I can apply to the admin change page or inline admin of any model.</p> <hr> <p>I have one model, <code>post</code> (not its real name) that is both an inline on the <code>blog</code> admin page, and also has its own admin page. The reason it can't just be inline is that it has models with foreign keys to it that only make sense when edited with it, and it only makes sense when edited with <code>blog</code>.</p> <p>For the <code>post</code> admin page, I changed part of "fieldset.html" from:</p> <pre><code>{% if field.is_readonly %} &lt;p&gt;{{ field.contents }}&lt;/p&gt; {% else %} {{ field.field }} {% endif %} </code></pre> <p>to</p> <pre><code>{% if field.is_readonly %} &lt;p&gt;{{ field.contents }}&lt;/p&gt; {% else %} {% ifequal field.field.name "blog" %} &lt;p&gt;{{ field.field.form.instance.blog_link|safe }}&lt;/p&gt; {% else %} {{ field.field }} {% endifequal %} {% endif %} </code></pre> <p>to create a link to the <code>blog</code> admin page, where <code>blog_link</code> is a method on the model:</p> <pre><code>def blog_link(self): return '&lt;a href="%s"&gt;%s&lt;/a&gt;' % (reverse("admin:myblog_blog_change", args=(self.blog.id,)), escape(self.blog)) </code></pre> <p>I couldn't find the <code>id</code> of the <code>blog</code> instance anywhere outside <code>field.field.form.instance</code>.</p> <p>On the <code>blog</code> admin page, where <code>post</code> is inline, I modified part of "stacked.html" from:</p> <pre><code>&lt;h3&gt;&lt;b&gt;{{ inline_admin_formset.opts.verbose_name|title }}:&lt;/b&gt;&amp;nbsp; &lt;span class="inline_label"&gt;{% if inline_admin_form.original %} {{ inline_admin_form.original }} {% else %}#{{ forloop.counter }}{% endif %}&lt;/span&gt; </code></pre> <p>to</p> <pre><code>&lt;h3&gt;&lt;b&gt;{{ inline_admin_formset.opts.verbose_name|title }}:&lt;/b&gt;&amp;nbsp; &lt;span class="inline_label"&gt;{% if inline_admin_form.original %} {% ifequal inline_admin_formset.opts.verbose_name "post" %} &lt;a href="/admin/myblog/post/{{ inline_admin_form.pk_field.field.value }}/"&gt; {{ inline_admin_form.original }}&lt;/a&gt; {% else %}{{ inline_admin_form.original }}{% endifequal %} {% else %}#{{ forloop.counter }}{% endif %}&lt;/span&gt; </code></pre> <p>to create a link to the <code>post</code> admin page since here I was able to find the <code>id</code> stored in the foreign key field.</p> <hr> <p><strong>I'm sure there is a better, more general way to do add links to admin forms without repeating myself; what is it?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload