Extra action form

../_images/actions.png

Extra Action Form

Admin Actions in daily usage often require one or more arguments. This extension provides an easy way to display a form to fill in what needs to be passed to the actions. Namely:

  1. Create a form that holds all the fields needed for any action and set action_form on your JumboModelAdmin pointing to that form.

    Caution

    Set any additional field as required = False otherwise all actions that don’t define that will will not let the form validate and you’ll get “No action selected”. See below the syntax to declare a required field just for one action

  2. Configure which fields are needed for any action (action_form_fields dict) and set attribute action_form_fields on your JumboModelAdmin

  3. Modify change_list.html template to add

    • templatetag to render admin/actions.html (inluded in {jmb.admin}result_list)
    • javascript to toggle visibility of the fields

    this step is already done in jmb.admin provided template

JumboModelAdmin.action_form

When ModelAdmin has actions enabled, Django creates a form and attaches it to attribute action_form, thought is not officially documented. This action will hold

  • the selected action
  • the selected ids or select_across boolean to say all records where selected.

Entending and modifying that form we make available any fields defined in it. The selected row will be available as a queryset, as usual.

You can use JumboModelAdmin.get_action_form_instance() to get an already validated form instance

This is used to specify a different form to add extra action fields. To use this you need override class JumboActionForm. It’s good idea define these forms in admin_forms.py or in admin/forms.py:

from jmb.admin.options import JumboActionForm

class NewsletterActionForm(JumboActionForm):
    mailing_list = forms.ModelChoiceField(
        queryset=MailingList.objects.filter(status=1),
        label=_('Mailing list'),
        required=False,
        # This is automatically added if not specified. Used from JS to toggle extra action fields
        widget=forms.Select(attrs={'id': 'mailing_list', 'class': 'extra_action_field'})
    )

JumboModelAdmin.action_form_fields

A dictionary to specify action and its extra fields to complete operation:

from jmb.admin.options import JumboModelForm, JumboActionForm

class NewsletterAdmin(JumboModelAdmin):

   def send_newsletter(self, request, queryset):
       form = self.get_form_action_instance(request)
       ...

   action_form = NewsletterActionForm
   action_form_fields = {
       'clone': [],
       'send_newsletter': ['mailing_list'],
       'resend_newsletter': ['mailing_list:required'],
    }

key is the action and value can be [] if there is not any extra fields or a list of extra fields. required it’s used to specify that that field is required. Example: send_newsletter: ['mailing_list:required']. When user select send newsletter action is required specify mailing_list to whom to send newsletter.

JumboModelAdmin.get_action_form_fields

It’s a method of JumboModelAdmin and it used to redefine action extra fields dictionary To use these methods we rewrote changelist_view in JumboModelAdmin adding get_action_form_fields to extra_context and we overrode change_list.html template.

API

class jmb.jadmin.options.JumboModelAdmin(model, admin_site)[source]
export_data_view(*args, **kwargs)[source]

The ‘export data’ admin view for this model.

get_action_form_fields(request)[source]

Return a dictionary to specify action and its extra fields to complete operation

get_action_form_instance(request)[source]

Return an action_form

get_changelist_instance(request)[source]

Return a chagelist instance suitable to find out the real queryset represented bu result_list This function can be used in actions when the queryset is much faster then using the list of single ids

get_changelist_queryset(request)[source]

Return a queryset that was correctly filtered by any filter (q= and filterset_class)

get_list_per_page()[source]

Set how many items appear on each paginated admin change list page. This value is conditioned by settings.LIST_PER_PAGE. Example:

LIST_PER_PAGE = {
‘content_content’: 50, ‘content’: 10, ‘all’: 100,

}

get_list_per_page_form(request)[source]

Returns number of items appear on each paginated admin change list page

import_data_view(*args, **kwargs)[source]

The ‘import data’ admin view for this model.

message_user(request, message, level=20, extra_tags='', fail_silently=False)[source]

Send a message to the user. The default implementation posts a message using the django.contrib.messages backend.

Exposes almost the same API as messages.add_message(), but accepts the positional arguments in a different order to maintain backwards compatibility. For convenience, it accepts the level argument as a string rather than the usual level number.

response_action(request, queryset)[source]

Handle an admin action. This is called if a request is POSTed to the changelist; it returns an HttpResponse if the action was handled, and None otherwise.

tabs_show_empty = True

tabs: show tabs even if they’re empty

tabs_sticky_on_top = False

tabs: show tab lables sticky on top of the page

Previous page

← Ajax inlines

This Page