From 48dd20473878fcdb30d49e62a877ca5d7fdb39e7 Mon Sep 17 00:00:00 2001 From: Daniel Greenfeld Date: Tue, 11 Jun 2013 21:48:52 +0200 Subject: [PATCH] Moar on actions --- docs/ref/actions.rst | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/ref/actions.rst b/docs/ref/actions.rst index 043bb64..74ccc99 100644 --- a/docs/ref/actions.rst +++ b/docs/ref/actions.rst @@ -2,11 +2,9 @@ Actions ======= -.. warning:: Incomplete and innaccurate! In the process of revising. -- pydanny - Actions are defined to work on a single view type. Currently, actions are only implemented against the ``ModelListView``. This view contains the default ``DeleteSelectedAction`` method, which in end functionality mirrors ``django.contrib.admin.delete_selected``. -However, under the hood, django-admin2's actions work very differently. Instead of functions with assigned attributes, they are full fledged objects. Which means you can more easily extend them to suit your needs. +However, under the hood, django-admin2's actions work very differently. Instead of functions with assigned attributes, they can either be functions or full fledged objects. Which means you can more easily extend them to suit your needs. The documentation works off a simple set of models, as listed below: @@ -41,7 +39,11 @@ The documentation works off a simple set of models, as listed below: Writing List Actions ----------------------- -Using our sample models, let's pretend we wrote a blog article about Django and our mother put in a whole bunch of embarressing comments. Rather than cherry-pick the comments, we want to delete the whole batch. +The basic workflow of Django’s admin is, in a nutshell, “select an object, then change it.” This works well for a majority of use cases. However, if you need to make the same change to many objects at once, this workflow can be quite tedious. + +In these cases, Django’s admin lets you write and register “actions” – simple functions that get called with a list of objects selected on the change list page. + +If you look at any change list in the admin, you’ll see this feature in action; Django ships with a “delete selected objects” action available to all models. Using our sample models, let's pretend we wrote a blog article about Django and our mother put in a whole bunch of embarressing comments. Rather than cherry-pick the comments, we want to delete the whole batch. In our blog/admin.py module we write: @@ -61,3 +63,16 @@ In our blog/admin.py module we write: djadmin2.default.register(Post, PostAdmin) djadmin2.default.register(Comment) + +.. warning:: + + The “delete selected objects” action uses `QuerySet.delete()`_ for efficiency reasons, which has an important caveat: your model’s delete() method will not be called. + + If you wish to override this behavior, simply write a custom action which accomplishes deletion in your preferred manner – for example, by calling ``Model.delete()`` for each of the selected items. + + For more background on bulk deletion, see the documentation on `object deletion`_. + +.. _`QuerySet.delete()`: https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.delete +.. _`Object deletion`: https://docs.djangoproject.com/en/dev/topics/db/queries/#topics-db-queries-delete + +Read on to find out how to add your own actions to this list. \ No newline at end of file