Skip to content

Instantly share code, notes, and snippets.

@aweakley
Created June 6, 2019 01:51
Show Gist options
  • Save aweakley/40ef5e1c8e199d7cce89984bcfc4be07 to your computer and use it in GitHub Desktop.
Save aweakley/40ef5e1c8e199d7cce89984bcfc4be07 to your computer and use it in GitHub Desktop.
Example readonly admin class for Django
# -*- coding: utf-8 -*-
from django.contrib import admin
class ReadonlyAdmin(admin.ModelAdmin):
"""
Example admin class which makes all fields readonly
"""
def __init__(self, model, admin_site):
super(ReadonlyAdmin, self).__init__(model, admin_site)
self.readonly_fields = [
field.name
for field in filter(lambda f: not f.auto_created, model._meta.fields)
]
def has_delete_permission(self, request, obj=None):
return False
def has_add_permission(self, request, obj=None):
return False
def changeform_view(self, request, object_id=None, form_url="", extra_context=None):
extra_context = extra_context or {}
extra_context["show_save_and_continue"] = False
extra_context["show_save"] = False
return super(ReadonlyAdmin, self).changeform_view(
request, object_id, extra_context=extra_context
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment