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.
Take a look at the Model Meta in the django documentation. Within a Model you can add class Meta this allows additional options for your model which handles things like singular and plural naming. Show activity on this post. inside model.py or inside your customized model file add class meta within a Model Class.
The simplest option is to set save_as=True on the ModelAdmin . This will replace the "Save and add another" button with a "Save as new" button.
Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With