Disable link to edit object in django's admin (display list only)?

后端 未结 12 482
渐次进展
渐次进展 2020-12-12 22:05

In Django\'s admin, I want disable the links provided on the \"select item to change\" page so that users cannot go anywhere to edit the item. (I am going to limit

相关标签:
12条回答
  • 2020-12-12 22:37

    I overrided get_list_display_links method and action to None.

    class ChangeLogAdmin(admin.ModelAdmin):
        actions = None
        list_display = ('asset', 'field', 'before_value', 'after_value', 'operator', 'made_at')
    
        fieldsets = [
            (None, {'fields': ()}),
        ]
    
        def __init__(self, model, admin_site):
            super().__init__(model, admin_site)
    
        def get_list_display_links(self, request, list_display):
            super().get_list_display_links(request, list_display)
            return None
    
    0 讨论(0)
  • 2020-12-12 22:42

    In your model admin set:

    list_display_links = (None,)
    

    That should do it. (Works in 1.1.1 anyway.)

    Link to docs: list_display_links

    0 讨论(0)
  • 2020-12-12 22:47

    just write list_display_links = None in your admin

    0 讨论(0)
  • 2020-12-12 22:50

    with django 1.6.2 you can do like this:

    class MyAdmin(admin.ModelAdmin):
    
        def get_list_display_links(self, request, list_display):
            return []
    

    it will hide all auto generated links.

    0 讨论(0)
  • 2020-12-12 22:51

    As user, omat, mentioned in a comment above, any attempt to simply remove the links does not prevent users from still accessing the change page manually. However, that, too, is easy enough to remedy:

    class MyModelAdmin(admin.ModelAdmin)
        # Other stuff here
        def change_view(self, request, obj=None):
            from django.core.urlresolvers import reverse
            from django.http import HttpResponseRedirect
            return HttpResponseRedirect(reverse('admin:myapp_mymodel_changelist'))
    
    0 讨论(0)
  • 2020-12-12 22:51

    In more "recent" versions of Django, since at least 1.9, it is possible to simple determine the add, change and delete permissions on the admin class. See the django admin documentation for reference. Here is an example:

    @admin.register(Object)
    class Admin(admin.ModelAdmin):
    
        def has_add_permission(self, request):
            return False
    
        def has_change_permission(self, request, obj=None):
            return False
    
        def has_delete_permission(self, request, obj=None):
            return False
    
    0 讨论(0)
提交回复
热议问题