这是一个有效的代码,但我不喜欢它的丑陋。请建议更多pythonic和elegent创建此列表的方法。比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}