我有一个shipment_details表单,其中包含有关装运的一些信息以及装运顺序中的所有项目。有两个模型信息一个发货另一个是项目模型。我想使用复选框更新所有Marco Item Shipped值(请参阅表单视图图片)。
这是我的表单视图
http://imgur.com/a/dcNZE
这是我的forms.py,其中我将项目链接到项目is_shipped字段,此值显示在视图中使用{{form_status.as_p}}
。
的 forms.py
class ShipmentStatus(forms.CheckboxInput):
input_type = 'checkbox'
class ShipmentStatusForm((forms.ModelForm)):
class Meta:
model = Items
fields = ['is_shipped']
widgets = {
'is_shipped': ShipmentStatus(),
}
这是我的视图模型 的 shipment_detail.html
{% extends "_dashboardlayout.html" %}
{% block content %}
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h2 class="page-header">Shipment Details</h2>
</div>
<div class="col-md-12">
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<table class="table table-striped">
<tr>
<th>Item No</th>
<th>SKU</th>
<th>Quantity</th>
<th>Price</th>
<th>Marco Item</th>
<th>Marco Item Shipped</th>
</tr>
{% for item in items_queryset %}
<tr>
<td>{{ item.item_no }}</td>
<td>{{ item.sku }}</td>
<td>{{ item.requested_quantity }}</td>
<td>{{ item.price }}</td>
<td>{{ item.is_send }}</td>
<td>{{ form_status.as_p }}</td>
</tr>
{% endfor %}
</table>
<input type="submit" value="Save">
</form>
</div>
</div>
<!-- /.row -->
</div>
{% endblock %}
这是我的控制py文件
def shipment_detail(request, order_id=None):
order_queryset = Order.objects.get(order_id=order_id)
customer_queryset = Customer.objects.all()
address_queryset = Address.objects.all()
items_queryset = Items.objects.filter(order_id=order_id).order_by('item_no')
shipment_queryset = Shipment.objects.filter(order_id=order_id)
# if there is no shipment data then generate shipment details for the Order
if not shipment_queryset:
form = ShipmentForm(request.POST or None)
form_status = ShipmentStatusForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
instance.order_id = order_queryset
order_queryset.save()
instance.save()
# save item status
if form_status.is_valid():
instance = form_status.save(commit=False)
instance.save()
context = {
"form": form,
"form_status": form_status,
"order_queryset": order_queryset,
"customer_queryset": customer_queryset,
"address_queryset": address_queryset,
"items_queryset": items_queryset,
}
return render(request, "shipment_detail.html", context)
# if there is already data then updated the shipment details for the Order
else:
instance = get_object_or_404(Shipment, order_id=order_id)
form = ShipmentForm(request.POST or None, instance=instance)
if form.is_valid():
instance = form.save(commit=False)
instance.order_id = order_queryset
# updated order is_shipped field according to ship_status
if instance.status == True:
order_queryset.is_shipped = True
if instance.status == False:
order_queryset.is_shipped = False
# updated order is_completed field according to shipment is_complete field
if instance.is_complete == True:
order_queryset.is_completed = True
if instance.is_complete == False:
order_queryset.is_completed = False
order_queryset.save()
instance.save()
print "form status"
# updated item is_shipped field
instance_status = get_list_or_404(Items, order_id=order_id)
for instance in instance_status:
form_status = ShipmentStatusForm(request.POST, instance=instance)
if form_status.is_valid():
instance = form_status.save(commit=False)
instance.save()
context = {
"form": form,
"instance": instance,
"form_status": form_status,
"order_queryset": order_queryset,
"customer_queryset": customer_queryset,
"address_queryset": address_queryset,
"items_queryset": items_queryset,
}
return render(request, "shipment_detail.html", context)
这里的问题是,当点击所有Marco项目发货时,如果正确地点击了它的保存值,如果我点击一个假,则另一个为真,那么它就不会保存价值。
答案 0 :(得分:2)
执行此操作的一种方法是使用对象的pk作为复选框的值
<input type="checkbox" value='{{item.id}}'
name='for_action' id='for_action' >
,您可以使用request.POST.getlist('for_action')
在这里你去!!
HTML:
<div class="col-lg-12">
<h2 class="page-header">Shipment Details</h2>
</div>
<div class="col-md-12">
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<table class="table table-striped">
<tr>
<th>Item No</th>
<th>SKU</th>
<th>Quantity</th>
<th>Price</th>
<th>Marco Item</th>
<th>Marco Item Shipped</th>
</tr>
{% for item in items_queryset %}
<tr>
<td>{{ item.item_no }}</td>
<td>{{ item.sku }}</td>
<td>{{ item.requested_quantity }}</td>
<td>{{ item.price }}</td>
<td>{{ item.is_send }}</td>
<td><input type="checkbox" value='{{item.id}}'
name='for_action' id='for_action' ></td>
</tr>
{% endfor %}
</table>
<input type="submit" value="Save">
</form>
</div>
</div>
<!-- /.row -->
{%endblock%}
views.py
def shipment_detail(request, order_id=None):
#########
# save item status
list_of_id_for_action = request.POST.getlist('for_action')
list_of_obj = Items.objects.filter(pk__in=list_of_id_for_action)
list_of_obj.update(is_shipped=True)
######
context = {
"form": form,
"instance": instance,
"form_status": form_status,
"order_queryset": order_queryset,
"customer_queryset": customer_queryset,
"address_queryset": address_queryset,
"items_queryset": items_queryset,
}
希望这有帮助