子钦加油

扩大
缩小

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

 

posted on 2020-03-27 15:34  子钦加油  阅读(...)  评论(...编辑  收藏

导航

返回顶部