如何筛选内联formset中的下拉列表

时间:2017-02-02 12:57:02

标签: python python-2.7 django-1.8 inline-formset

我的内联formset包含一个带有可用项目的dropdon列表。我正在尝试按日期过滤此列表。不幸的是,我提供的查询集并不限制选择:

def order(request, date):
    new_order = get_object_or_404(Order, date=date, user=None)
    new_items = Order_items.objects.filter(order__date=date, quantity=0)
    OrderFormset = inlineformset_factory(Order, Order_items, extra=0, fields='__all__')
    ordering_user = request.user
    if request.method == 'POST':
        form = OrderForm(request.POST, instance=new_order)
        formset = OrderFormset(request.POST, instance=new_order, queryset=Order_items.objects.filter(order__date=date, quantity=0))
        [...]
    else:
        form = OrderForm(instance=new_order)
        formset = OrderFormset(instance=new_order, queryset=Order_items.objects.filter(order__date=date, quantity=0))
    [...]

相反,它显示表Order_items中可用的每条记录。我应该改变什么才能使它发挥作用?

1 个答案:

答案 0 :(得分:0)

好的,解决方案是在行下方添加的简单查询:

for n in formset:
            n.fields['product'].queryset = Menu.objects.filter(date=date)