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()
  • 删除、更新操作见下图

Django 对多对多字段进行批量add_第1张图片

 

你可能感兴趣的:(Django 对多对多字段进行批量add)