How to make Django model just view (read-only) in Django Admin?
django, django-admin, django-models
Solution
You need to set `list_display_links` attribute of your ModelAdmin class to `(None,)`. But this can be done only in `__init__` after standard ModelAdmin `__init__` call otherwise it will throw `ImproperlyConfigured` exception with text `... list_display_links[0]' refers to 'None' which is not defined in 'list_display'`. And you should define `has_add_permisssion` anyway to hide add button:
class AmountOfBooksAdmin(admin.ModelAdmin):
actions = None # disables actions dropbox with delete action
list_display = ('book', 'amount')
def has_add_permission(self, request):
return False
def __init__(self, *args, **kwargs):
super(AmountOfBooksAdmin, self).__init__(*args, **kwargs)
self.list_display_links = (None,)
# to hide change and add buttons on main page:
def get_model_perms(self, request):
return {'view': True}
To hide 'view' and 'change' buttons from madin admin page you must place index.html from django/contrib/admin/templates/admin/ to you templates dir /admin and change it:
{% for model in app.models %}
...
{% if model.perms.view %}
<th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
{% else %}
{% if model.admin_url %}
<th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
{% else %}
<th scope="row">{{ model.name }}</th>
{% endif %}
{% endif %}
....
Problem
This question is continuation of How to remove Add button in Django admin, for specific Model? I have realized that my first question was not formulated good, so I tough that it is better that I start new question, that to fix the old one. Because there was already some answers. So question is how to make Django Model that will be view read only. So that you can not add new, delete old, change current, but that you also do not have button for that on web admin UI. Solution from first question are all related to fields, but not to whole model. They all work, in sense that you will not be able to edit those field, but I am not satisfied how they do it. Current solution are: - Use `readonly_fields` on all fields from model -> I do not like it because you can click on row to change it, but you can not edit fields. - `editable=False` on filed definition -> This will not show the field on web admin UI. But you can still click on row, but you will just not see anything, and still have Save button. - `def has_add_permission(self, request):` -> same as 2 - don't give anyone the add permission for this model -> same as 2 Any thoughts ?