admin.py:

......
from django import forms
from django.contrib.admin.helpers import ActionForm
from clever_selects.form_fields import ChainedChoiceField, ChoiceField
from clever_selects.forms import ChainedChoicesForm
from django.contrib import messages
............
class BatchActionForm(ActionForm):
    profile = ChoiceField(label=u'Profile', choices=[], required=False)
    location = ChainedChoiceField(empty_label=_(u'---- Choose ----'),
                                 parent_field='profile',
                                 ajax_url='/ajax/custom-chained-view-url/',
                                 required=False)
 
    batch = ChainedChoiceField(empty_label=_(u'---- Choose ----'),
                              parent_field='location',
                              ajax_url='/ajax/custom-chained-view-url2/',
                              required=False)
 
     def __init__(self, *args, **kwargs):
        # I evaluate the profile field here, because when you add a profile
        # the list won't update unless you evaluate it here in __init__
        p = Profile.objects.filter(profile_type=2)
            .values_list('id', 'name')
        p_list = [(None, u'---- Choose ----')]
        p_list.extend(p)
        super(BatchActionForm, self).__init__(*args, **kwargs)
        self.fields['profile'] = forms.ChoiceField(choices=p_list,
                           label=u'Profile',
                           required=False,
                           widget=forms.Select(attrs={'id': 'id_profile'}))

 

To be continued…

Advertisement