Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin- disable Editing and remove "Save" buttons for a specific model

Tags:

I have a Django Model which I wish to be only readonly. No adds and edits allowed.

I have marked all fields readonly and overridden has_add_permission in ModelAdmin as:

class SomeModelAdmin(admin.ModelAdmin):      def has_add_permission(self, request):         return False 

Is there a similar has_edit_permission? Which can be disabled to remove "Save" and "Save and continue" buttons? And replace by a simple "Close and Return" button.

Django Documentation Only mentions only about read only fields not about overriding edit permissions.

like image 243
jerrymouse Avatar asked Dec 09 '11 08:12

jerrymouse


People also ask

How do I restrict access to parts of Django admin?

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 you remove save and add another Django admin?

The simplest option is to set save_as=True on the ModelAdmin . This will replace the "Save and add another" button with a "Save as new" button.

How can I remove extra's from Django admin panel?

You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category , the admin displays Categorys , but by adding the Meta class, we can change it to Categories . Save this answer. Show activity on this post.


1 Answers

For Django 1.11:

def has_add_permission(self, request, obj=None):     return False  def changeform_view(self, request, object_id=None, form_url='', extra_context=None):     extra_context = extra_context or {}     extra_context['show_save_and_continue'] = False     extra_context['show_save'] = False     return super(YourModelAdmin, self).changeform_view(request, object_id, extra_context=extra_context) 
like image 134
xleon Avatar answered Sep 27 '22 23:09

xleon