- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法从 unit_id
中获取值在用户完成选择并发布数据之后。有人可以帮我解决这个问题。unit_id
的值下拉列表是从另一个数据库表 ( LiveDataFeed
) 获得的。一旦选择了一个值并发布了表单,它就会给出错误:
选择一个有效的选项。该选择不是可用的选择之一。
这是实现:
在models.py中:
class CommandData(models.Model):
unit_id = models.CharField(max_length=50)
command = models.CharField(max_length=50)
communication_via = models.CharField(max_length=50)
datetime = models.DateTimeField()
status = models.CharField(max_length=50, choices=COMMAND_STATUS)
class CommandSubmitForm(ModelForm):
iquery = LiveDataFeed.objects.values_list('unit_id', flat=True).distinct()
unit_id = forms.ModelChoiceField(queryset=iquery, empty_label='None',
required=False, widget=forms.Select())
class Meta:
model = CommandData
fields = ('unit_id', 'command', 'communication_via')
def CommandSubmit(request):
if request.method == 'POST':
form = CommandSubmitForm(request.POST)
if form.is_valid():
form.save()
return HttpResponsRedirect('/')
else:
form = CommandSubmitForm()
return render_to_response('command_send.html', {'form': form},
context_instance=RequestContext(request))
最佳答案
你得到了一个平面 value_list ,它只是一个 id 列表,但是当你这样做时,你可能最好使用一个普通的 ChoiceField
而不是 ModelChoiceField
并为它提供一个元组列表,而不仅仅是 id。例如:
class CommandSubmitForm(ModelForm):
iquery = LiveDataFeed.objects.values_list('unit_id', flat=True).distinct()
iquery_choices = [('', 'None')] + [(id, id) for id in iquery]
unit_id = forms.ChoiceField(iquery_choices,
required=False, widget=forms.Select())
ModelChoiceField
,并使用
LiveDataFeed.objects.all()
作为查询集,但为了在框中显示 id 并为选项值填充它,您必须子类化
ModelChoiceField
覆盖
label_from_instance
方法。你可以看到一个
example in the docs here .
关于django 表单给出 : Select a valid choice. 该选项不是可用选项之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8536107/
我正在使用 Python2 和 Django 1.9。 基本上我有一个包含两个下拉菜单的表单。第二个下拉列表取决于第一个下拉列表的值。 例如,如果下拉列表 #1 选择了“类别”选项,则下拉列表 #2
我有 2 个 Django 模型——主要(产品)和链接的详细信息(PhysicalProperty 是通过附加模型 ProductMetricals 的多对多链接)。 在主模型 Product 中,我
我正在尝试创建一个带有两个下拉菜单的网站:部门和类(class)编号。下拉菜单的数据来 self 的 SQL 数据库的“类(class)”表。现在我的网站正确初始化并在下拉菜单中显示正确的选项。然而,
你好,我对 python 相当陌生,我的一段代码遇到了问题。在一个“if”语句之后,当我不希望它执行时,我的代码将继续执行下一个“if”语句。我尝试过 elif 但它提出了无效的语法。 print(
我已经在我的 Django 项目中安装了 MongoDB。因为是我第一次使用 Mongo,我决定尝试一下它是如何工作的,我创建了一个简单的程序来存储数据(在我的例子中是价格和数量)。 该项目称为“ex
我已经尝试过 ActionListener 和 ItemListener,但它不起作用,而且我也尝试过搜索可能适用于它的代码和方法,但我只是没有找到它,主要可能是因为 JCombobox 更受欢迎,我
抱歉,我是编程界的新手。我正在创建一个控制台应用程序,它基本上会根据您的输入来确定您是想将摄氏温度转换为华氏温度,还是相反。 我遇到的问题是: "Error 3 A local variable na
random.SystemRandom().choice() 和有什么区别& random.choice()在 python ? 我有 seen the former being used ,不止一处
我的 XSD 有问题。我的 XSD 包含一个 xs:choice 来实现选择其中一种类型的选项。不允许选择无类型(或 Null)! 我已经通过 xsd.exe 从 xsd
我有 2 个应用程序。 input:具有包含下拉列表的形式:region。下拉列表值来自结果数据库(由用户上传)。填写表单时,用户需要从下拉列表中选择值。 result:有数据库 现在我可以在输入表单
有人可以告诉我我可能做错了什么吗?我在运行 python 代码时不断收到此消息: import random foo = ['a', 'b', 'c', 'd', 'e'] random_item =
为什么 numpy.random.choice 与 random.choice 不同?当我这样做时: >>> random.choice([(1,2),(4,3)]) (1, 2) 有用。 但是当
我有一个由 2 个 ChoiceField 和一个 CharField 组成的表单。当选择第一个 ChoiceField 的项目时,通过 ajax 填充第二个 ChoiceField。 填充它的模板如
为什么我们不能在表单中使用Choice.IMPLICIT,而我们可以使用Choice.EXCLUSIVE。当我使用 Choice.IMPLICIT 时,它给了我一个 java.lang.Illegal
例子: element = ['Flaming', 'Cold'] fire_properties = ['of Fire', 'of Flame'] cold_properties = ['of I
我正在生成我们的 Java Web 服务 WSDL,然后每次进行更改时将其导入到我们的 C# 应用程序中。每次这样做时,xs:choice元素会重新生成,但通常具有不同的数量,具体取决于首先生成的元素
我是 Django 新手。我使用简单的 ajax 根据类(class)选择动态更新选择字段学期。但是在提交表单时,我收到错误选择有效的选择。 所选选项不是可用选项之一。代码如下: 表单.py: fro
1)是否有symfony方法? 我有一个基本形式(未映射到数据库),其中包含一些选择字段,例如: $builder->add('civility', 'choice', array('cho
我已经为这个问题苦苦挣扎了一段时间,所以非常感谢任何帮助。 情况如下:我的应用程序有一个名为 InitialViewController 的 UIViewController 子类。这个 View C
这个问题在这里已经有了答案: How can I dynamic populate an option widget in tkinter depending on a choice from a
我是一名优秀的程序员,十分优秀!