4

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:

  1. 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.
  2. 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.
  3. def has_add_permission(self, request): -> same as 2
  4. don't give anyone the add permission for this model -> same as 2

Any thoughts ?

Community
  • 1
  • 1
WebOrCode
  • 6,852
  • 9
  • 43
  • 70

1 Answers1

3

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 %}

        ....
ndpu
  • 22,225
  • 6
  • 54
  • 69
  • this is partially correct. Problem is that on main page on http://127.0.0.1:8000/admin/ there is still "Add" and "Change" buttons . Clicking on change button bring you to list of books, what is OK, but anyway I thing that "Change" button should not be shown. But when you click on "Add" button you get "403 Forbiden" page which is not so good. If you know how to remove this "Add" and "Change" button please say. Than you on your help so far. – WebOrCode Dec 03 '13 at 10:20