Imaginemos un mundo libre

La paz interior comienza en el momento en el que decides no permitir, que ninguna persona o evento, tome el control de tus emociones.

Upgrade to Django 1.1

with one comment

Developing or considering Django to create your cool 2.0 web applications/sites?

Please, try the new version of Django (1.1) which comes with interesting new features.

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:

/usr/local/lib/python$version/dist-packages/
/usr/local/lib/python$version/site-packages/
or
/usr/lib/python$version/dist-packages/
/usr/lib/python$version/site-packages/

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.

Example:

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:

Django admin actions

Django admin actions

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:

After performing admin actions in Django

After performing admin actions in Django

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:

  • Any field in list_editable must be in list_display.
  • The same field can’t be listed in both list_editable and list_display_links
  • 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.

    Django - list_editable option

    Django - list_editable option

    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.

    Written by Ronny Yabar

    August 3, 2009 at 7:46 am

    Posted in Django

    One Response

    Subscribe to comments with RSS.

    1. Thanks for your article. I am new at python and this will be a big help.

      Joni Wilmoth

      March 6, 2010 at 8:02 pm


    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: