django: Modify the model admin form for a specific model

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:

{% extends 'admin/change_form.html' %}

{% block after_related_objects %}
{{ block.super }}

Oh harro!
{% 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.

class MyModelAdmin(admin.ModelAdmin):
# A template for changing the model admin form
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