Reputation: 1930
I've been trying to automate the creation of a series of admin actions in Django.
Basically I want to create the possibility to bulk change a status field on a Project object. The status field is a ForeignkeyField.
I thought making a Class like this would work:
class StatusAction(object):
def __init__(self,status):
self.status = status
def make_action(self, modeladmin, request, queryset):
self.queryset = queryset.update(status=self.status)
make_action.short_description = "Change status to '%s' for selected projects" % status
and then declare the actions like :
actions = [StatusAction(s.id).make_action for s in Status.objects.all()]
I am encountering two problems:
I also tried doing this with a closure (function in a function). It solves the attribute of a function problem, but still only one action ends up in the admin.
Upvotes: 0
Views: 929
Reputation: 3631
As I understand the action functions should have different names:
def create_action(status):
def action_func(modeladmin, request, queryset):
print status
action_func.__name__ = 'make_action_%d' % status.id
action_func.short_description = "Change status to '%s' for selected projects" % status
return action_func
actions = []
for s in Status.objects.all():
actions.append(create_action(s))
Upvotes: 3