Django admin - make all fields readonly

后端 未结 9 1168
春和景丽
春和景丽 2020-12-05 17:56

I\'m trying to make all fields readonly without listing them explicitly.

Something like:

class CustomAdmin(admin.ModelAdmin):
    def get_readonly_fi         


        
9条回答
  •  有刺的猬
    2020-12-05 18:22

    Since django 2.1, you can prevent editing, while allowing viewing, by returning False from the ModelAdmin's has_change_permission method, like this:

    class CustomAdmin(admin.ModelAdmin):
        def has_change_permission(self, request, obj=None):
            return False
    

    (This will not work before django 2.1, as it will also deny permission to any user trying only to view.)

提交回复
热议问题