输入输入字段时,尽管整个模型是正确的,但仍会引发错误python django

dced5bon  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(132)

表单未通过验证并抛出错误,尽管输入了正确的数据,但问题是什么?
我在输入字段中输入 +79211234569 并在html中给出一个错误,请选择正确的选项。您的选项不在有效值中。
表格数据: <'form': <RelatedAddForm bound=True, valid=False, fields=(name;phone)> 形式

class ListTextWidget(forms.Select):
    template_name = 'include/_forms_clients_datalist.html'

    def format_value(self, value):
        # Copied from forms.Input - makes sure value is rendered properly
        if value == '' or value is None:
            print('ListTextWidget None')
            return ''
        if self.is_localized:
            print('ListTextWidget local')
            return formats.localize_input(value)
        return str(value)

class ChoiceTxtField(forms.ModelChoiceField):
    widget=ListTextWidget()

class RelatedAddForm(forms.ModelForm):
    phone = ChoiceTxtField(queryset=Clients.objects.order_by('-phone'))

    class Meta:
        model = Clients
        fields = ['name', 'phone']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control', 'autocomplete': 'off'}),
        }

模型

class Clients(models.Model):
    name = models.CharField(max_length=150, blank=True, verbose_name='Имя')
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    phone = models.CharField(validators=[phone_regex], unique=True, max_length=17, verbose_name='Телефон')  

    def get_absolute_url(self):
        return reverse('view_clients', kwargs={'pk': self.pk})

    def __str__(self):
        return self.phone

upd:
请寄 <QueryDict: {'csrfmiddlewaretoken': ['m9mKTv4kLWSFmW6Jj39OUAZ0zINBoFjvphjYADWvY97lk1oKAB3LAHhxOpmXnKbo'], 'cli ents-name': ['test'], 'clients-phone': ['+79121234566']}> 通过 name 变量和 phone 变量的格式正确
最可能的问题是,电话字段的检查与 ChoiceTxtField ?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题