Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin "Edit Selection" Action?

I'd like to write a django-admin action (for use when the user selects zero or more rows) that will allow them to edit the selected items as a group. I only need to edit one of the items in the model (the "room") at a time, but I don't want to have to go through all 480 of my objects and manually edit them one-by-one.

Is there a way to throw up an interstitial page that allows the user to edit the items as a group?

like image 461
magneticMonster Avatar asked Dec 29 '25 02:12

magneticMonster


1 Answers

You are able to create custom admin actions, and using JavaScript or custom ModleForms you could easily create popup windows or alerts, or whatever you want to do. For example I have this in the admin for one of my apps:

admin.py:

def deactivate_selected(modeladmin, request, queryset):
    rows_updated = queryset.update(active=0)
    for obj in queryset: obj.save()
    if rows_updated == 1:
        message_bit = '1 item was'
    else:
        message_bit = '%s items were' % rows_updated
    modeladmin.message_user(request, '%s successfully deactivated.' % message_bit)
deactivate_selected.short_description = "Deactivate selected items"

## add deactivates 
admin.site.add_action(deactivate_selected)

This adds the option to "Deactive selected items" in the admin page.

It seems to me that it would be easy to make a custom action to "Update room for selected items" that would present a JavaScript prompt, take that input, and provide it to the custom action function to perform what you need to do.

More reading can be found on this here: Writing Django Admin Actions.

like image 110
jathanism Avatar answered Dec 31 '25 15:12

jathanism