django admin readonly models

Solutions on MaxInterview for django admin readonly models by the best coders in the world

showing results for - "django admin readonly models"
Alan
27 Nov 2016
1"""
2source: https://archive.is/AiLh1
3license: https://archive.is/iCoSO
4"""
5
6
7class ReadOnlyAdminMixin:
8    """Disables all editing capabilities."""
9    change_form_template = "admin/view.html"
10
11    def __init__(self, *args, **kwargs):
12        super(ReadOnlyAdminMixin, self).__init__(*args, **kwargs)
13        self.readonly_fields = [f.name for f in self.model._meta.get_fields()]
14
15    def get_actions(self, request):
16        actions = super(ReadOnlyAdminMixin, self).get_actions(request)
17        actions.pop("delete_selected", None)
18        return actions
19
20    def has_add_permission(self, request):
21        return False
22
23    def has_delete_permission(self, request, obj=None):
24        return False
25
26    def save_model(self, request, obj, form, change):
27        pass
28
29    def delete_model(self, request, obj):
30        pass
31
32    def save_related(self, request, form, formsets, change):
33        pass
34
35@register(models.Address)
36class AddressAdmin(ReadOnlyAdminMixin, admin.ModelAdmin):
37    list_display = ('country', 'state', 'city', 'zipcode')
38    search_fields = ('country', 'state', 'city', 'zipcode')
39    list_filter = ('country', 'state')