Moar on actions

This commit is contained in:
Daniel Greenfeld 2013-06-11 21:48:52 +02:00
parent eefab2dec4
commit 48dd204738

View file

@ -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 Djangos 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, Djangos 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, youll 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 models 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.