Skip to content

Commit

Permalink
Added chapter for removing delete button
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 9, 2018
1 parent f2fa7ef commit 464cca6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/add_actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ How to add additional actions in Django admin?
+++++++++++++++++++++++++++++++++++++++++++++++

Django admin allows you to add additional actions which allow you to do bulk actions.
You have been asked to add an action which will mark multiple :code:`Hero`s as immortal.
You have been asked to add an action which will mark multiple :code:`Hero` s as immortal.

You can do this by adding action a method to ModelAdmin
and adding the method as string to :code:`actions`::
and adding the method as string to :code:`actions` ::

actions = ["mark_immortal"]

Expand Down
21 changes: 21 additions & 0 deletions docs/remove_add_delete.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
How to remove the 'Add'/'Delete' button for a model?
====================================================


The UMSRA management has added all the Category and Origin objects and wants to disable any further addition and deletion.
They have asked you to disable 'Add' and 'Delete' buttons. You can do this by overriding the
:code:`has_add_permission` and :code:`has_delete_permission` in the Django admin.::



def has_add_permission(self, request):
return False

def has_delete_permission(self, request, obj=None):
return False


With these changes, the admin looks like this

.. image:: remove_add_perms.png

Note the removed `Add` buttons. The add and delete buttons also get removed from the detail pages. You can also read :doc:`remove_delete_selected`.

Binary file added docs/remove_add_perms.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/remove_delete_selected.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ We can remove it form the dropdown. Your code looks like this with the changes.:
And your admin looks like this

.. image:: export_selected.png

You should also read :doc:`remove_add_delete`.
12 changes: 12 additions & 0 deletions heroes_and_monsters/entities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,23 @@ class VillainAdmin(admin.ModelAdmin, ExportCsvMixin):
class CategoryAdmin(admin.ModelAdmin):
list_display = ("name",)

def has_add_permission(self, request):
return False

def has_delete_permission(self, request, obj=None):
return False


@admin.register(Origin)
class OriginAdmin(admin.ModelAdmin):
list_display = ("name", "hero_count", "villain_count")

def has_add_permission(self, request):
return False

def has_delete_permission(self, request, obj=None):
return False

def get_queryset(self, request):
queryset = super().get_queryset(request)
queryset = queryset.annotate(
Expand Down

0 comments on commit 464cca6

Please sign in to comment.