Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin: separate read-only view and change view

I'd like to use the django admin to produce a read-only view of an object which contains an "Edit" button which switches you to the usual change view of the same object.

I know how to use the readonly attributes to produces a read-only view, but I don't know how to produce two views, one read-only and one that allows changes.

I'd like to reuse as much of the admin interface for this as possible, rather than writing a view from scratch.

Note that this question isn't about permissions: all users will have permission to change the objects. It's just that I would prefer that they not use the change_view unless they do intend to make changes, reducing the risk of accidental changes or simultaneous changes.

like image 725
Dan Christensen Avatar asked Jul 13 '11 14:07

Dan Christensen


People also ask

How do I make a Django admin read only?

Django admin by default shows all fields as editable and this fields option will display its data as-is and non-editable. we use readonly_fields is used without defining explicit ordering through ModelAdmin or ModelAdmin. fieldsets they will be added. Now we make the Question text fields read-only.

Can we customize Django admin panel?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.

How do I restrict access to admin pages in Django?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

How do I add permissions to Django admin?

Test the 'view' permission is added to all modelsUsing #3 for Django 1.7 only creates the permission objects if the model doesn't already exist. Is there a way to create a migration (or something else) to create the permission objects for existing models?


1 Answers

Here's an answer that literally does what I asked with only a few lines of code and just a couple of template changes:

class MyModelAdmin(admin.ModelAdmin):     fieldsets = [...]      def get_readonly_fields(self, request, obj=None):         if 'edit' not in request.GET:             return <list all fields here>         else:             return self.readonly_fields 

Now the usual URL for the change_form will produce a read only change_form, but if you append "?edit=1" to the URL, you will be able to edit.

The change_form template can also be customized depending on whether "?edit=1" is in the URL. To do this, put 'django.core.context_processors.request' in TEMPLATE_CONTEXT_PROCESSORS in settings.py, and then use request.GET.edit in the template.

For example, to add an "Edit" button when not in edit mode, insert

  {% if not request.GET.edit %}                                                    <li><a href="?edit=1">Edit</a></li>                                              {% endif %}  

just after <ul class="object-tools"> in change_form.html.

As another example, changing change_form.html to contain

{% if save_on_top and request.GET.edit %}{% submit_row %}{% endif %}             

will mean that the submit row will only be shown in edit mode. One can also hide the Delete buttons on inlines, etc, using this method.

For reference, here is what I put in settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (                                                      'django.contrib.auth.context_processors.auth',                                   'django.core.context_processors.debug',                                          'django.core.context_processors.i18n',                                           'django.core.context_processors.media',                                          'django.contrib.messages.context_processors.messages',                           # Above here are the defaults.                                                   'django.core.context_processors.request',                                    ) 
like image 114
Dan Christensen Avatar answered Oct 03 '22 04:10

Dan Christensen