Django Override Admin change_form.html Template - display associated model in template

Joe J picture Joe J · Oct 8, 2010 · Viewed 15.2k times · Source

I have a situation where I've overrided the admin_change.html template for a given model. I've created a file such as:

/myproject/templates/admin/myapp/mymodel/change_form.html

Then, in the overrided change_form.html template, where I am editing an existing object instance, I want to have access to that model instance variable so I can display more information about it.

{% extends "admin/change_form.html" %}
{% block after_field_sets %}{{ block.super }}
Print my model here: {{ mymodel }}
Print foreignkey related records of my model:  
    {% for item in mymodel.items_set.all %} {{ item }} {% endfor %}
{% endblock %}

However, I don't know what the template variable is called that I should use to access this model (if it is even passed at all). I've tried digging through the admin source code, but get lost quickly. Does anyone know how to access this model instance variable from within an extended django template?

(NOTE: in the above code, the reference to {{ mymodel }} is incorrect. But the point is that I want to be able to use a variable like that in my template code to reference the mymodel instance.)

Any advice is much appreciated. Thanks, Joe

Answer

Joe J picture Joe J · Oct 9, 2010

Looks like I found a means to do this using this syntax.

{% extends "admin/change_form.html" %}
{% block after_field_sets %}{{ block.super }}
Print my model here {{ original }}
Print foreignkey related records of my model:
{% for item in original.items_set.all %} {{ item }} {% endfor %}
{% endblock %}

Sorry to answer so soon, but thanks to anyone who started researching. Perhaps someone will find this helpful. If you see a better way to do this, feel free to comment.