用 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()
写成这样不行啊。。。。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.