Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 847
Joe J Avatar asked Oct 08 '10 21:10

Joe J


2 Answers

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.

like image 178
Joe J Avatar answered Oct 18 '22 22:10

Joe J


Just poking around, it seems that {{adminform.form.instance}} works.

like image 17
fooo Avatar answered Oct 19 '22 00:10

fooo