Last active
April 3, 2018 17:37
-
-
Save andreif/0eb6cf9cfbd1056283dd35afe9f5c653 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from django.contrib import admin | |
| class ReadOnlyAdminMixin: | |
| def get_readonly_fields(self, request, obj=None): | |
| fields = list(self.fields or []) + \ | |
| [f.name for f in self.model._meta.fields] | |
| _id = self.model._meta.pk.name | |
| fields = [_id] + list({f: f for f in fields if f != _id}.keys()) | |
| return fields | |
| def has_add_permission(self, request): | |
| return False | |
| def has_delete_permission(self, request, obj=None): | |
| return False | |
| class ReadOnlyInlineMixin(ReadOnlyAdminMixin): | |
| extra = 0 | |
| show_change_link = True | |
| max_num = 0 | |
| can_delete = False | |
| def read_only_inline(model): | |
| class ReadOnlyInline(ReadOnlyInlineMixin, admin.TabularInline): | |
| pass | |
| ReadOnlyInline.model = model | |
| return ReadOnlyInline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment