Readonly models in Django admin interface?

前端 未结 14 1576
北恋
北恋 2020-12-02 05:53

How can I make a model completely read-only in the admin interface? It\'s for a kind of log table, where I\'m using the admin features to search, sort, filter etc, but there

14条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 06:15

    with django 2.2, readonly admin can be as simple as:

    class ReadOnlyAdminMixin():
        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
    
    
    class LogEntryAdmin(ReadOnlyAdminMixin, admin.ModelAdmin):
        list_display = ('id', 'user', 'action_flag', 'content_type', 'object_repr')
    

提交回复
热议问题