Django Multivaluefield可选字段

时间:2018-02-10 22:20:20

标签: python django django-forms

我已经搜遍了所有我能找到的东西是多年前的东西和/或那些不适用的东西。

我尝试在表单中添加MultiValueField,以便人们可以轻松输入一到三个输入。只需要这些字段中的第一个,但是表单验证器仍然需要全部三个字段,除非我将整个事物设为可选的(它不应该是)。

下面是我的MultiWidget,MultiValueField和表单的代码。我尝试删除除required=False之外的字段属性中的所有内容,但它仍然需要所有内容。我在调用字段而不是字段require_all_fields=False时尝试在表单中设置__init__,但仍然需要所有字段。

我觉得我已经阅读,阅读和阅读,而且关于如何实现此类事情的信息非常少。

class ThreeNumFields(forms.MultiWidget):
    def __init__(self, attrs=None):
        self.widgets = [
            forms.TextInput(),
            forms.TextInput(),
            forms.TextInput()
        ]
        super().__init__(self.widgets, attrs)

    def decompress(self, value):
        if value:
            return value.split(' ')
        return [None, None]


class LocationMultiField(forms.MultiValueField):
    widget = ThreeNumFields()
    validators = [RegexValidator]

    def __init__(self):
        fields = (
            forms.CharField(
                error_messages={'incomplete': 'Please enter at least one valid zip code.'},
                validators=[
                    RegexValidator(r'^[0-9]{5}$', 'Enter a valid US zip code.'),
                ],
                max_length=5,
            ),
            forms.CharField(
                required=False,
                validators=[
                    RegexValidator(r'^[0-9]{5}$', 'Enter a valid US zip code.'),
                ],
                max_length=5,
            ),
            forms.CharField(
                required=False,
                validators=[
                    RegexValidator(r'^[0-9]{5}$', 'Enter a valid US zip code.'),
                ],
                max_length=5,
            )
        )
        super(LocationMultiField, self).__init__(
            fields=fields,
            require_all_fields=False,
        )

    def compress(self, data_list):
        return ' '.join(data_list)


class NewForecastForm(forms.ModelForm):
    class Meta:
        model = ForecastProfile
        exclude = ['userid', 'last_updated']

    nickname = forms.CharField(
        label=_('Forecast Nickname')
    )
    locations = LocationMultiField()
    timezone = forms.ChoiceField(
        label=_('Timezone for your forecast'),
        choices=choices.timezones
    )
    start_time = forms.ChoiceField(
        label=_('Earliest time you want in your forecast'),
        help_text=_('(Time will not be exact to account for timezone conversions and forecast data.)'),
        choices=choices.times
    )
    end_time = forms.ChoiceField(
        label=_('Latest time you want in your forecast'),
        help_text=_('(Time will not be exact to account for timezone conversions and forecast data.)'),
        choices=choices.times
    )
    alerts = forms.MultipleChoiceField(
        choices=choices.alerts,
        widget=forms.CheckboxSelectMultiple()
    )
    days_in_forecast = forms.ChoiceField(
        choices=choices.forecastdays
    )

    def clean(self):
        cleaned_data = super(NewForecastForm, self).clean()
        start = cleaned_data.get("start_time")
        end = cleaned_data.get("end_time")

        if start > end:
            raise forms.ValidationError(
                "Your start time must be before your end time."
            )

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,它看起来像是Django的一个错误。这是一张开放票,#29205

也就是说,我确实通过设置required=Falserequire_all_fields=False并在窗体的init方法中向窗口小部件attrs明确添加所需的窗体来使表单正常工作。

在您的情况下,这意味着将self.fields['locations'].widget.widgets[0].attrs['required'] = True放在NewForecastForm

的init方法中