Created
June 6, 2019 01:51
-
-
Save aweakley/40ef5e1c8e199d7cce89984bcfc4be07 to your computer and use it in GitHub Desktop.
Example readonly admin class for Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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