Skip to content

Commit

Permalink
Added remove delete selected chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 9, 2018
1 parent 9a7ba29 commit f2fa7ef
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/add_actions.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
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 can do this by adding action a method to ModelAdmin
and adding the method as string to :code:`actions`::

actions = ["mark_immortal"]

def mark_immortal(self, request, queryset):
queryset.update(is_immortal=True)

2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ Bulk and custom actions
:maxdepth: 1
:numbered:

add_actions
export
import
remove_delete_selected
add_actions
action_buttons

Permissions
Expand Down
Binary file added docs/remove_delete_selected.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions docs/remove_delete_selected.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
How to remove the delete selected action in Django admin?
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

By default Django adds a `Delete Selected` action to the listview page. You have been asked to remove
the action from the :code:`Hero` admin.

The method :code:`ModelAdmin.get_actions` returns the actions shown. By overriding this method, to remove :code:`delete_selected`
We can remove it form the dropdown. Your code looks like this with the changes.::

def get_actions(self, request):
actions = super().get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions

And your admin looks like this

.. image:: export_selected.png
9 changes: 8 additions & 1 deletion heroes_and_monsters/entities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ def export_as_csv(self, request, queryset):
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin):
list_display = ("name", "is_immortal", "category", "origin", "is_very_benevolent")
list_filter = ("is_immortal", "category", "origin", IsVeryBenevolentFilter)
actions = ["export_as_csv"]
actions = ["mark_immortal"]

def mark_immortal(self, request, queryset):
queryset.update(is_immortal=True)

def get_actions(self, request):
actions = super().get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions

def is_very_benevolent(self, obj):
return obj.benevolence_factor > 75
Expand Down

0 comments on commit f2fa7ef

Please sign in to comment.