Задать вопрос

Тел: +7 965 3737 888

449

Просмотров

2

Ответов

TableSelectMultiple Widget

<p>A widget for selecting from a list of Model instances using MultipleChoiceField which renders a table row for each choice, consisting of a column for a checkbox followed by a column for each item specified in item_attrs, which must specify attributes of the objects passed as choices.</p>

Вопрос полезен? Да0/Нет0
file_2446.py(2.7Кб)
None

Ответы (2):

Ответstfs:17.01.2012
Ответ полезен? Да0/Нет0

Thanks.

I added the following code to support dictionaries as well:

     54   for attr in self.item_attrs:
     55       if isinstance(item, dict):
     56           content = item[attr]
     57       elif callable(attr):

Please tell me if this could introduce any problems that I'm not seeing.

Ответinsin:19.12.2007
Ответ полезен? Да0/Нет0

Example form:

class SelectJobsForInvoiceForm(forms.Form):
    """
    A Form which handles selection of Jobs for invoice.
    """
    jobs = forms.MultipleChoiceField(widget=TableSelectMultiple(
        item_attrs=('formatted_number', 'name', 'client',
                    'get_status_display')))

    def __init__(self, accessible_jobs, *args, **kwargs):
        super(SelectJobsForInvoiceForm, self).__init__(*args, **kwargs)
        self.fields['jobs'].choices = [(j.id, j) \
                                       for j in accessible_jobs]

    def clean(self):
        print self.cleaned_data
        if 'jobs' not in self.cleaned_data or \
           len(self.cleaned_data['jobs']) == 0:
            raise forms.ValidationError(
                u'You must select at least one Job for invoice.')
        return self.cleaned_data