Django表单始终与ChoiceField无效 - 选择有效选择

时间:2017-09-08 18:21:42

标签: python django forms

我最近在我的表单中添加了一个ChoiceField,但现在它总是返回无效;我用'print(form.is_valid())'验证了这一点。所有字段都在我的模板中正确显示,并且值总是在传递,所以我无法理解为什么表单返回为无效。

models.py

class Lead(models.Model):
    Company_Name = models.CharField(max_length=180, default="No_Name", blank=True)
    Company_ID = models.BooleanField(default=True)
    Client_Name = models.CharField(max_length=180, default="No_Name_Provided", blank=True)
    Client_ID = models.BooleanField(default=True)
    Advertising = models.BooleanField(default=False)
    Automotive = models.BooleanField(default=False)
    Storage = models.BooleanField(default=False)
    Child_Related = models.BooleanField(default=False)
    Cleaning = models.BooleanField(default=False)
    Coffee = models.BooleanField(default=False)
    Computer = models.BooleanField(default=False)
    Internet = models.BooleanField(default=False)
    Dry_Cleaning = models.BooleanField(default=False)
    Education = models.BooleanField(default=False)
    Employment = models.BooleanField(default=False)
    Financial_Services = models.BooleanField(default=False)
    Fitness = models.BooleanField(default=False)
    Food = models.BooleanField(default=False)
    Eco_Friendly = models.BooleanField(default=False)
    Hair_Care_Beauty_Salon = models.BooleanField(default=False)
    Health = models.BooleanField(default=False)
    Nutrition = models.BooleanField(default=False)
    Home_Services = models.BooleanField(default=False)
    Maintenance = models.BooleanField(default=False)
    Management = models.BooleanField(default=False)
    Training = models.BooleanField(default=False)
    Miscellaneous = models.BooleanField(default=False)
    Pack_and_Mail = models.BooleanField(default=False)
    Pet_Related = models.BooleanField(default=False)
    Print_Copy = models.BooleanField(default=False)
    Real_Estate = models.BooleanField(default=False)
    Repair_Restoration = models.BooleanField(default=False)
    Retail = models.BooleanField(default=False)
    Senior_Care = models.BooleanField(default=False)
    Sports = models.BooleanField(default=False)
    Tanning_Salon = models.BooleanField(default=False)
    Travel = models.BooleanField(default=False)
    Vending = models.BooleanField(default=False)
    Home_Based = models.BooleanField(default=False)
    Investment_Level = models.CharField(max_length=2, choices=CATEGORIES, default=CATEGORIES[0])

forms.py

class FormForReq(forms.Form):
    Company_Name = forms.CharField(max_length=180, required=False)
    Client_Name = forms.CharField(max_length=180, required=False)
    category = forms.BooleanField(initial=False, required=False)
    From_Home = forms.BooleanField(initial=False, required=False)
    Advertising = forms.BooleanField(initial=False, required=False)
    Automotive = forms.BooleanField(initial=False, required=False)
    Storage = forms.BooleanField(initial=False, required=False)
    Child_Related = forms.BooleanField(initial=False, required=False)
    Cleaning = forms.BooleanField(initial=False, required=False)
    Coffee = forms.BooleanField(initial=False, required=False)
    Computer = forms.BooleanField(initial=False, required=False)
    Internet = forms.BooleanField(initial=False, required=False)
    Dry_Cleaning = forms.BooleanField(initial=False, required=False)
    Education = forms.BooleanField(initial=False, required=False)
    Employment = forms.BooleanField(initial=False, required=False)
    Financial_Services = forms.BooleanField(initial=False, required=False)
    Fitness = forms.BooleanField(initial=False, required=False)
    Food = forms.BooleanField(initial=False, required=False)
    Eco_Friendly = forms.BooleanField(initial=False, required=False)
    Hair_Care_Beauty_Salon = forms.BooleanField(initial=False, required=False)
    Health = forms.BooleanField(initial=False, required=False)
    Nutrition = forms.BooleanField(initial=False, required=False)
    Home_Services = forms.BooleanField(initial=False, required=False)
    Maintenance = forms.BooleanField(initial=False, required=False)
    Management = forms.BooleanField(initial=False, required=False)
    Training = forms.BooleanField(initial=False, required=False)
    Miscellaneous = forms.BooleanField(initial=False, required=False)
    Pack_and_Mail = forms.BooleanField(initial=False, required=False)
    Pet_Related = forms.BooleanField(initial=False, required=False)
    Print_Copy = forms.BooleanField(initial=False, required=False)
    Real_Estate = forms.BooleanField(initial=False, required=False)
    Repair_Restoration = forms.BooleanField(initial=False, required=False)
    Retail = forms.BooleanField(initial=False, required=False)
    Senior_Care = forms.BooleanField(initial=False, required=False)
    Sports = forms.BooleanField(initial=False, required=False)
    Tanning_Salon = forms.BooleanField(initial=False, required=False)
    Travel = forms.BooleanField(initial=False, required=False)
    Vending = forms.BooleanField(initial=False, required=False)
    Home_Based = forms.BooleanField(initial=False, required=False)
    Investment_Level = forms.ModelChoiceField(queryset=CATEGORIES, required=False)

这是在线数据库,因此我使用modelform创建项目和表单以搜索并拉取结果。因此,我需要两者兼顾。

编辑:

我发现它选择了一个无效的选择。想法如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

ModelChoiceField应该引用查询集来生成其选择。在您的示例中,您显示:

Investment_Level = forms.ModelChoiceField(queryset=CATEGORIES,)

您不会显示CATEGORIES是什么,但通常会执行以下操作:

Investment_Level = forms.ModelChoiceField(queryset=Foo.objects.all(),)

如果您确实希望下拉列表显示来自常量的硬编码选项,则应使用常规ChoiceField,而不是ModelChoiceField。

答案 1 :(得分:0)

  

这是在线数据库,因此我使用modelform创建项目

不,你不是。这就是为什么你有这么多重复的代码和问题。使用实际的ModelForm

from .models import Lead

class FormForReq(forms.ModelForm):
    class Meta:
        model = Lead
        fields = '__all__'