用 django form 提交表单怎么批量对 m2m 字段进行保存啊??
ValueError: "<OrderShipping: OS-180623085540>" needs to have a value for field "id" before this many-to-many relationship can be used.
class OrderGoodsInfo(models.Model):
...
....
class OrderShipping(models.Model):
order = models.ManyToManyField(OrderGoodsInfo, blank=True, verbose_name="物流 ID")
....
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()
写成这样不行啊。。。。