def formfield(self, **kwargs):
        """
        This returns the correct formclass without calling super

        Returns select_multiple_field.forms.SelectMultipleFormField
        """
        defaults = {
            'required': not self.blank,
            'label': capfirst(self.verbose_name),
            'help_text': self.help_text
        }
        if self.has_default():
            if callable(self.default):
                defaults['initial'] = self.default
                defaults['show_hidden_initial'] = True
            else:
                defaults['initial'] = self.get_default()

        if self.choices:
            # Django normally includes an empty choice if blank, has_default
            # and initial are all False, we are intentially breaking this
            # convention
            include_blank = self.blank
            defaults['choices'] = self.get_choices(include_blank=include_blank)
            defaults['coerce'] = self.to_python
            if self.null:
                defaults['empty_value'] = None

            # Many of the subclass-specific formfield arguments (min_value,
            # max_value) don't apply for choice fields, so be sure to only pass
            # the values that SelectMultipleFormField will understand.
            for k in kwargs.keys():
                if k not in ('coerce', 'empty_value', 'choices', 'required',
                             'widget', 'label', 'initial', 'help_text',
                             'error_messages', 'show_hidden_initial'):
                    del kwargs[k]

        defaults.update(kwargs)
        return forms.SelectMultipleFormField(**defaults)
Exemplo n.º 2
0
class TimeTableForm(forms.ModelForm):
    days = f.SelectMultipleFormField(required=False, choices=DAY_CHOICES)

    class Meta:
        model = TimeTable
        exclude = ('camera', 'status', 'user', 'group', 'interval_length')