I want to enable/disable history from django admin button based on the type of user.
My end goal here is to be able to understand how to show hide this button.
I want to enable/disable history from django admin button based on the type of user.
My end goal here is to be able to understand how to show hide this button.
Unfortunately, Django does not provide an easy way to toggle History button like it is done for 'Add' button, for instance. The easiest way would be to overwrite a change_form.html and remove the next lines from block object-tools-items:
<li>
{% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
<a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>
</li>
Keep in mind that you have to specify change_form for every admin model.
Example:
class TestAdmin(admin.ModelAdmin):
# path to the app_name/templates/admin/app_name/change_form.html
change_form_template = 'admin/app_name/change_form.html'
# Register your models here.
admin.site.register(Test, TestAdmin)
A clean solution would be to override change_form_object_tools.html template, which needs to be placed in templates/admin/ of your project.
{% load i18n admin_urls %}
{% block object-tools-items %}
{% block comment %}
<li>
{% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
<a href="{% add_preserved_filters history_url %}" class="historylink">
{% translate "History" %}</a>
</li>
{% endcomment %}
{% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="viewsitelink">{% translate "View on site" %}</a></li>{% endif %}
{% endblock %}