django: Modify the model admin form for a specific model

0 Comments

The automatically generated django admin forms are already quite customisable already, but sometimes you need to add a new element or piece of information that just isn't part of the form or model.

That's where its easier to override the template a little.

Place the file in a template path in your project or app, in the following directory structure and filename:

"admin/your_app_label/your_model_name/change_form.html"

The model name should be in lower case.

Now in the template file:

1.{% extends 'admin/change_form.html' %}
2. 
3.{% block after_related_objects %}
4.{{ block.super }}
5. 
6.Oh harro!
7.{% endblock after_related_objects %}

In your django installation folder, see "contrib\admin\templates\admin\change_form.html" for what block names you can override.

Another method would be to specify the template file in the model.

1.class MyModelAdmin(admin.ModelAdmin):
2.  # A template for changing the model admin form
3.  change_form_template = "myapp/blah_change_form.html"

Just remember, do not override too much stuff. Stick to modifying small blocks and always remember to display {{ block.super }} in case anything changes in future revisions.

peniscolada12
A simple example of what happens when you change too much.

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog