#12660 closed (duplicate)
Proposal: adding "render_display_form" support to ModelAdmin.change_view (per row granular permission system)
Reported by: | Massimiliano | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 1.2-alpha |
Severity: | Keywords: | row permission view change display | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I found often useful in my programs to endow ModelAdmin with the ability to just display the content of a Model instance.
Let me describe you the scenario:
you have a model with an editor field and a status field.
Editors can only modify the instance if it is still in "draft" or "proposed_for_approval" statuses, but not once it is published; they should be able to search and display published (a.k.a. editable only by superusers) instances.
I use filter items shown by ModelAdmin.change_list using defining an ad-hoc .queryset method; I define has_add/change/delete_permission and a "new" has_display_permission() too. Then when an editor clicks on an item in the change_list for which he has no change permission, instead of automatically raising a Permission Denied exception, I check if it's the case to display the instance. So inside ModelAdmin.change_view I replaced:
if not self.has_change_permission(request, obj): raise PermissionDenied }}} with {{{ if not self.has_change_permission(request, obj): if not self.has_display_permission(request, obj): raise PermissionDenied else: display_or_change = 'display' else: display_or_change = 'change'
The last line of ModelAdmin.change_view changes from return self.render_change_form(request, context, change=True, obj=obj)
to {{{
if display_or_change == 'change':
return self.render_change_form(request, context, change=True, obj=obj)
else:
return self.render_display_form(request, context, obj=obj, behave_as='object_detail') }}}
The ModelAdmin.display_form is basicly a kind of django.views.generic.list_detail.object_detail. The parameter behave_as instructs render_display_form if it has search for a TEMPLATE_DIR/APP/MODEL_detail.html or a more generic self.display_form_template or TEMPLATE_DIR/(%s/%s/)display_form.html the same way ModelAdmin.render_change_form does.
Duplicate of #820