Dynamically create Django admin actions

 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 4 years, 9 months ago
  2. Generic csv export admin action by dek 4 years, 8 months ago
  3. merge action in django admin by yeago 3 years, 6 months ago
  4. Admin action for a generic "CSV Export" by javinievas 3 years, 2 months ago
  5. Admin actions as buttons instead of a menu [v2] by itavor 2 years, 4 months ago

Comments

mssobhan (on April 24, 2011):

Thanks for the help.

#

(Forgotten your password?)