gpt4 book ai didi

python - Django Multivaluefield 可选字段

转载 作者:行者123 更新时间:2023-11-28 18:15:21 26 4
gpt4 key购买 nike

我到处搜索,我能找到的都是几年前的东西和/或不适用的东西。

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

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

我觉得我读了又读又读,关于如何实现这类事情的信息很少。

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."
)

最佳答案

我遇到了同样的问题,它看起来像是 Django 的一个错误。有一张未结票,#29205 .

就是说,我确实通过设置 required=Falserequire_all_fields=False 并显式将 required 添加到小部件属性中,从而使表单在我的情况下正常工作表单的初始化方法。

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

关于python - Django Multivaluefield 可选字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48726178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com