Django 对多对多字段进行批量add
class OrderGoodsInfo(models.Model):
…
…
class OrderShipping(models.Model):
order = models.ManyToManyField(OrderGoodsInfo, blank=True, verbose_name=“物流ID”)
…
views.py
container_orders_id = []
for order in container_obj.orders.all():
container_orders_id.append(order.id)
order_goods = OrderGoodsInfo.objects.filter(id__in=container_orders_id)
order_shipping = OrderShipping(user=request.user)
form = OrdershippingForm(request.POST, instance=order_shipping)
if form.is_valid():
osp = form.save(commit=False)
osp.order.add(*order_goods)
osp.save()
删除、更新操python基础教程作见下图
作者:子钦加油
出处:https://www.cnblogs.com/zmdComeOn/