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

Deadly 提交于 2019-11-29 07:17:07

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/3894493/django-override-admin-change-form-html-template-display-associated-model-in-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!