Login

Dynamically create Django admin actions

Author:
pantsman
Posted:
December 14, 2009
Language:
Python
Version:
1.1
Tags:
admin admin-actions closures
Score:
3 (after 3 ratings)

With this snippet, I made a set of admin actions for assigning Quality objects to Package objects.

The Django docs for ModelAdmin.get_actions explain the dictionary of tuples that is returned.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def create_action(quality):
    def action(modeladmin, request, queryset): queryset.update(quality=quality)
    name = "mark_%s" % (quality,)
    return (name, (action, name, "Mark selected as %s quality" % (quality,)))

class PackageAdmin(admin.ModelAdmin):
    list_display = ('name', 'quality')
            
    def get_actions(self, request):
        return dict(create_action(q) for q in models.Quality.objects.all())

More like this

  1. Export Related as JSON Admin Action by johnboxall 5 years, 9 months ago
  2. Generic csv export admin action by dek 5 years, 8 months ago
  3. merge action in django admin by yeago 4 years, 6 months ago
  4. Admin action for a generic "CSV Export" by javinievas 4 years, 2 months ago
  5. Admin actions as buttons instead of a menu [v2] by itavor 3 years, 4 months ago

Comments

mssobhan (on April 24, 2011):

Thanks for the help.

#

Please login first before commenting.