简体   繁体   English

Django表单:“选择一个有效的选择。 该选择不是可用的选择之一。”

[英]Django Form: “Select a valid choice. That choice is not one of the available choices.”

I'm working with Python2 and Django 1.9. 我正在使用Python2和Django 1.9。

Basically I have a form that contains two dropdowns. 基本上,我有一个包含两个下拉菜单的表单。 The second dropdown depends on the value of the first one. 第二个下拉列表取决于第一个下拉列表的值。

For exampe, if dropdown #1 has the option "Category" selected, then dropdown #2 should display options "CategoryA, CategoryB and CategoryC". 例如,如果下拉列表1选择了选项“类别”,则下拉列表2应显示选项“类别A,类别B和类别C”。 In the same way, if dropdown#1 has the option "Department" selected, Dropdown#2 should show "Department1, Department2, Department3". 同样,如果下拉列表1选择了“部门”选项,则下拉列表2应显示“部门1,部门2,部门3”。

Notice that both "Departments" and "Categories" are classes with their corresponding database tables. 请注意,“部门”和“类别”都是具有相应数据库表的类。

So here comes my question. 所以这是我的问题。 How to define that form? 如何定义表格? More especifcally, how do I indicate that the second form will sometimes display objects from the class Category and sometimes objects of the class Department ? 更具体地说,如何指示第二种形式有时显示Category类中的对象,有时显示Department类中的对象?

This is what I have so far: 这是我到目前为止的内容:

class MyClassForm(forms.Form):]
    name = forms.CharField(max_length=255)
    dropdown1 = forms.ModelChoiceField(
        queryset=TypeOfCriteria.objects.all().order_by('name'))
    dropdown2 = forms.ModelChoiceField(
        queryset=Department.objects.none())

Notice how I've defined dropdodown2 : 注意我如何定义dropdodown2

    dropdown2 = forms.ModelChoiceField(
        queryset=Department.objects.none())

How should I define the value of the parameter queryset for dropdown2 ? 如何为dropdown2定义参数queryset的值? Since I have to specify the class that is going to be queried to obtain the list of all of its instances, how should I do it? 由于我必须指定要查询的类以获取其所有实例的列表,因此该怎么办?

Right now, I'm loading the content of dropdown2 with JQuery. 现在,我正在使用JQuery加载dropdown2的内容。 But when I hit the "send" button to send the post data I'm always getting the error: "Select a valid choice. That choice is not one of the available choices." 但是,当我按下“发送”按钮发送帖子数据时,总是收到错误消息:“选择一个有效的选择。该选择不是可用的选择之一。”

One option would be to update the queryset dynamically in the __init__ method of the form. 一种选择是使用表单的__init__方法动态更新查询集。 Keep the rest of your form class as is, then add this code: 保持表单类的其余部分不变,然后添加以下代码:

def __init__(self, *args, **kwargs):
    super(MyClassForm, self).__init__(*args, **kwargs)
    if 'dropdown1' in self.data:
        self.fields['dropdown2'].queryset = Department.objects.filter(typeofcriteria=self.data['dropdown1'])

In the init method 在init方法中

    def __init__(self,  *arts,  **kwargs):
      super(MyClassForm, self).__init__(*args, **kwargs) 
      self.fields['dropdown2'].queryset = Department.objects.none()
      if self.is_bound:
         self.fields['dropdown2'].queryset = Department.objects.filter(# any value in self.data) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择一个有效的选项。 [&quot;objects&quot;] 不是可用的选择之一。 在姜戈 - Select a valid choice. ["objects"] is not one of the available choices. in Django 选择一个有效的选项。 该选择不是可用的选择之一。 Django 表单出错 - Select a valid choice. That choice is not one of the available choices. error with Django Form Django 选择一个有效的选择。[...] 不是可用的选择之一。 以动态生成的形式 - Django Select a valid choice.[...] is not one of the available choices. in a dynamically generated form 选择一个有效的选择。[ <selection> ]不是可用的选择之一。 错误 - Select a valid choice.[<selection>]is not one of the available choices. Error Django表单错误:选择一个有效的选项。 ......不是可用的选择之一 - Django Form Error: Select a valid choice. … is not one of the available choices ModelChoiceFilter 给出“选择一个有效的选择。 这种选择不是可用的选择之一。” 提交时 - ModelChoiceFilter gives “Select a valid choice. That choice is not one of the available choices.” when submitted 选择一个有效的选项。 该选择不是可用的选择之一——Django 表单 - Select a valid choice. That choice is not one of the available choices --Django forms Django:Select 是一个有效的选择。 该选择不是可用的选择之一 - Django: Select a valid choice. That choice is not one of the available choices DjangoFilterBackend:过滤主键会导致“选择一个有效的选择。该选择不是可用的选择之一。” - DjangoFilterBackend: Filtering on a primary key results in "Select a valid choice. That choice is not one of the available choices." django modelformset_factory 引发表单无效:id 选择一个有效的选择。 该选择不是可用的选择之一 - django modelformset_factory raises form not valid: id Select a valid choice. That choice is not one of the available choices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM