django表单给:选择一个有效的选择。这种选择不是可用的选择之一 [英] django forms give: Select a valid choice. That choice is not one of the available choices

查看:1779
本文介绍了django表单给:选择一个有效的选择。这种选择不是可用的选择之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户完成选择并发布数据后,我无法从 unit_id 中捕获值。有人可以帮我解决这个问题。

I am unable to catch the values from the unit_id after the selection is done by the user and data is posted. Can someone help me to solve this.

unit_id 下拉列表的值是从另一个数据库表( LiveDataFeed )。一旦选择了一个值并将其发布,就会出现错误:

The values of the unit_id drop down list is obtained from another database table (LiveDataFeed). And once a value is selected and form posted, it gives the error:

选择一个有效的选择。这个选择不是可用的选择之一。

Select a valid choice. That choice is not one of the available choices.

这是实现:

在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)  

在views.py:

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


推荐答案

p>你得到一个平面的value_list,它只是一个ids列表,但是当你这样做时,你可能更好地使用一个简单的 ChoiceField 的一个 ModelChoiceField ,并为其提供一个元组列表,而不仅仅是ids。例如:

You're getting a flat value_list back which will just be a list of the ids, but when you do that, you're probably better off using a plain ChoiceField instead of a ModelChoiceField and providing it with a list of tuples, not just ids. For example:

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 方法。您可以在这里的文档中看到示例

You could also leave it as a ModelChoiceField, and use LiveDataFeed.objects.all() as the queryset, but in order to display the id in the box as well as have it populate for the option values, you'd have to subclass ModelChoiceField to override the label_from_instance method. You can see an example in the docs here.

这篇关于django表单给:选择一个有效的选择。这种选择不是可用的选择之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆