如何过滤内联表单集中的下拉列表
How to filter dropdown list in inline formset
我的内联表单集包含一个包含可用项目的下拉列表。我正在尝试按日期过滤此列表。不幸的是,我提供的查询集不限制选择:
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))
[...]
相反,它显示 table Order_items 中可用的每条记录。我应该更改什么才能使其正常工作?
好的,解决方案是在以下行下方添加一个简单的查询:
for n in formset:
n.fields['product'].queryset = Menu.objects.filter(date=date)
我的内联表单集包含一个包含可用项目的下拉列表。我正在尝试按日期过滤此列表。不幸的是,我提供的查询集不限制选择:
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))
[...]
相反,它显示 table Order_items 中可用的每条记录。我应该更改什么才能使其正常工作?
好的,解决方案是在以下行下方添加一个简单的查询:
for n in formset:
n.fields['product'].queryset = Menu.objects.filter(date=date)