这是一个工作代码,但我不喜欢它的丑陋。请提出更多创建此列表的pythonic和优雅的方式。比 Django 问题更多的 Python。这是代码:
order = Order.objects.get(pk=pk)
items = Item.objects.all()
if request.method == 'POST':
#OrderItem model has a foreign field "order" to Order model
instance = OrderItem.objects.filter(order=order)
items_forms = [OrderItemForm(request.POST, prefix=str(x), instance=instance[x]) for x in
range(0, Item.objects.count())]
#saving forms here
return redirect('/step2/')
items_forms = [ItemForm(prefix=str(x)) for x in range(0, Item.objects.count())]
return {'forms': items_forms , 'items': items}