Upgrade to Django 1.1
Developing or considering Django to create your cool 2.0 web applications/sites?
If you already have Django installed, you must remove the old version directory before upgrading. The path to the directory depends of your system, but it is usually located at:
One of the new features I recently tried is the possibility to create admin actions that allow for bulk updates to many objects at once.
Imagine you have a field called status which is a boolean field and you want to change the status of many elements at the same time. It’s annoying to select an object one by one to change the status. Here come “admin actions”, they are really useful.
In this short example, I want to make some objects published/unpublished. I will define two methods in my admin.py to achieve this.
def published(modeladmin, request, queryset): queryset.update(status=1) published.short_description = 'Published stores' def unpublished(modeladmin, request, queryset): queryset.update(status=0) unpublished.short_description = 'Unpublished stores'
As you can see, in this case, the actions take an argument: queryset, which contains the action to perform and the fields that will be affected.
Then, you add these actions to your model admin:
class StoreAdmin(admin.ModelAdmin): ... actions = [published,unpublished]
And your admin interface will look like this:
Now you can select Published/Unpublished and press “Go” to test the actions.
I selected some elements to make them unpublished and in a second I got the following:
Simple right?. KISS is always important. But, there are some things to consider before finishing:
Those actions(published, unpublished) are placed outside the class ModelAdmin. (StoreAdmin in this example). But, we also can define these actions as methods of the ModelAdmin.
The advantage is that defining actions as methods gives the action a more direct access to the ModelAdmin (Performance) and the possibility to call any of the methods provided by the admin.
So, I am going to define the actions as methods of StoreAdmin and rename modeladmin parameter to self.
And finally I put the strings ‘published’,’unpublished’ in actions to tell the ModelAdmin to see those actions as methods instead of direct references to functions:
class StoreAdmin(admin.ModelAdmin): ... actions = ['published','unpublished'] def published(self, request, queryset): queryset.update(status=1) published.short_description = 'Published stores' def unpublished(self, request, queryset): queryset.update(status=0) unpublished.short_description = 'Unpublished stores'
And that’s it. If you want to know more about admin actions or implement advanced action techniques, take a look at the excellent Django documentation.
Another feature I tried in Django 1.1 was the option: list_editable which allow edit and save multiple rows at once from the change list page. This option has the same format that list_display, but you have two restrictions:
class StoreAdmin(admin.ModelAdmin): ... list_display = ('customer', 'state','city','phone','status') list_editable = ['phone']
A screenshot of the list_editable option for the field phone of my Store model.
Well, I’ll continue exploring the new features in Django 1.1. If you have something to share, don’ t hesitate to post a comment or blog about that.