Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 497365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:46:28+00:00 2026-05-13T05:46:28+00:00

I am trying to dynamically build a list of admin actions using the get_actions()

  • 0

I am trying to dynamically build a list of admin actions using the get_actions() method on a ModelAdmin. Each action relates to a particular instance of another model, and as new instances may be added or removed, I want to make sure the list of actions reflects that.

Here’s the ModelAdmin:

class PackageAdmin(admin.ModelAdmin):
    list_display = ('name', 'quality')

    def _actions(self, request):
        for q in models.Quality.objects.all():
            action = lambda modeladmin, req, qset: qset.update(quality=q)
            name = "mark_%s" % (q,)
            yield (name, (action, name, "Mark selected as %s quality" % (q,)))

    def get_actions(self, request):
        return dict(action for action in self._actions(request))

(The weird repetitive dict of tuples return value is explained by the Django docs for get_actions().)

As expected, this results in a list of appropriately named admin actions for bulk assignment of Quality foreign keys to Package objects.

The problem is that whichever action I choose, the same Quality object gets assigned to the selected Packages.

I assume that the closures I am creating with the lambda keyword all contain a reference to the same q object, so every iteration changes the value of q for every function.

Can I break this reference, allowing me to still use a list of closures containing different values of q?


Edit: I realise that lambda is not necessary in this example. Instead of:

action = lambda modeladmin, req, qset: qset.update(quality=q)

I could simply use def:

def action(modeladmin, req, qset):
    return qset.update(quality=q)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:46:28+00:00Added an answer on May 13, 2026 at 5:46 am

    try

       def make_action(quality):
            return lambda modeladmin, req, qset: qset.update(quality=quality)
    
       for q in models.Quality.objects.all():
           action = make_action(q)
           name = "mark_%s" % (q,)
           yield (name, (action, name, "Mark selected as %s quality" % (q,)))
    

    if that doesn’t work, i suspect the bug has something to do with your use of yield. maybe try:

    def make_action(quality):
        name = 'mark_%s' % quality
        action = lambda modeladmin, req, qset: qset.update(quality=quality)
        return (name, (action, name, "Mark selected as %s quality" % quality))
    
    def get_actions(self, request):
        return dict([make_action for q in models.Quality.objects.all()])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a dynamically generated unordered list in the following format
I am trying to dynamically build a table on my page through ajax, using
I'm trying to build an associative array in PHP dynamically, and not quite getting
I am trying to dynamically create a Bitmap Image using byte array using following
I am trying to create a treeview dynamically using c# and asp.net. I have
So I'm trying to dynamically build a table in javascript and I already have
How can I dynamically build a list of mappings - instead of: class UrlMappings
I'm new to JQuery and trying to use it to dynamically build HTML based
I'm trying to dynamically build a javascript array where _tags is a globally defined
I'm trying to construct a table with dynamically build information. My thought was to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.