Make a Django model read-only?

前端 未结 4 1697
感情败类
感情败类 2021-01-08 00:21

What it says on the tin. Is there a way to make a Django model read-only?

By this I mean a Django model in which once records have been created, they can\'t be edite

4条回答
  •  渐次进展
    2021-01-08 01:16

    In addition to other solutions: If your main goal is to avoid write access from the admin, you can modify the used admin class so that nobody has an add/change permission:

    class HistoryAdmin(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
    

提交回复
热议问题