From be65f6536a0a342d9eee2be7ecafcccd6a018979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 14 Feb 2017 01:21:51 +0100 Subject: [PATCH 1/3] Add a `get_changelist_form` hook in the ModelAdmin This method is used in the `changelist_view` to get the changelist form. You may want to override this method to get a different form depending on the user that makes the request. For example: class MyConstanceAdmin(ConstanceAdmin): def get_changelist_form(self, request): if request.user.is_superuser: return SuperuserForm: else: return super(MyConstanceAdmin, self).get_changelist_form(request) --- constance/admin.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/constance/admin.py b/constance/admin.py index 6315364..c18ce5f 100644 --- a/constance/admin.py +++ b/constance/admin.py @@ -184,14 +184,23 @@ class ConstanceAdmin(admin.ModelAdmin): return config_value + def get_changelist_form(self, request): + """ + Returns a Form class for use in the changelist_view. + """ + # Defaults to self.change_list_form in order to preserve backward + # compatibility + return self.change_list_form + @csrf_protect_m def changelist_view(self, request, extra_context=None): if not self.has_change_permission(request, None): raise PermissionDenied initial = get_values() - form = self.change_list_form(initial=initial) + form_cls = self.get_changelist_form(request) + form = form_cls(initial=initial) if request.method == 'POST': - form = self.change_list_form(data=request.POST, initial=initial) + form = form_cls(data=request.POST, initial=initial) if form.is_valid(): form.save() messages.add_message( From 49656c055c943d8e5a81de4b4c7c7b8707336b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 14 Feb 2017 02:20:26 +0100 Subject: [PATCH 2/3] Updates the doc according to the last commit --- docs/index.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/index.rst b/docs/index.rst index 4fd6645..b1c3d3b 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -332,6 +332,24 @@ settings the way you like. admin.site.unregister([Config]) admin.site.register([Config], ConfigAdmin) +You can also override the ``get_changelist_form`` method which is called in +``get_changelist_view`` to get the actual form used to change the settings. This +allows you to pick a different form according to the user that makes the +request. For example: + +.. code-block:: python + + class SuperuserForm(ConstanceForm): + # Do some stuff here + + class MyConstanceAdmin(ConstanceAdmin): + def get_changelist_form(self, request): + if request.user.is_superuser: + return SuperuserForm: + else: + return super(MyConstanceAdmin, self).get_changelist_form(request) + +Note that the default method returns ``self.change_list_form``. More documentation ------------------ From cb3df533c8b3aaaebab5033f32880862e7c16af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 14 Feb 2017 02:28:47 +0100 Subject: [PATCH 3/3] typo --- docs/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.rst b/docs/index.rst index b1c3d3b..749fd0f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -333,7 +333,7 @@ settings the way you like. admin.site.register([Config], ConfigAdmin) You can also override the ``get_changelist_form`` method which is called in -``get_changelist_view`` to get the actual form used to change the settings. This +``changelist_view`` to get the actual form used to change the settings. This allows you to pick a different form according to the user that makes the request. For example: