美文网首页
2019-10-18 Django 添加包含 ManytoMan

2019-10-18 Django 添加包含 ManytoMan

作者: 多吃水果少吃肉 | 来源:发表于2019-10-18 17:02 被阅读0次
    @list_route(methods=['POST'])
    def outcomes(self, request, **kwargs):
        # print(request.data)
        if isinstance(request.data, dict):
            # print(request.data)
            reagent_list = []
            split_reagent_list = []
            outcome_list = []
            inner_samples = []
            flow = None
            for k, v in request.data.items():
                if k == 'reagent':
                    for item in v:
                        suit = ReagentSuit.objects.filter(id=item["suit"]).first()
                        reagent = Reagent(suit=suit, batch_number=item["batch_number"], usage=item["usage"], surplus=item["surplus"], remark=item["remark"])
                        reagent.save()
                        reagent_list.append(reagent)
                if k == 'split':
                    for item in v:
                        split_reagent = SplitReagent(name=item["name"], usage=item["usage"], split=item["split"], surplus=item["surplus"], remark=item["remark"])
                        split_reagent.save()
                        split_reagent_list.append(split_reagent)
                if k == 'outcome':
                    for item in v:
                        if "flow" in item and not flow:
                            if item["flow"]:
                                flow = Flow.objects.filter(id=item["flow"]).first()
                        inner_samples.append(InternalSample.objects.filter(id=item["isid"]).first())
                        otype = Outcome.objects.filter(id=item["otype"]).first()
                        outcome = OutcomeValue(otype=otype, number=item["number"])
                        outcome.save()
                        outcome_list.append(outcome)
            user = User.objects.filter(username=request.user.username).first()
            outcome_log = OutcomeLog.objects.create(flow=flow, operator=user)
            outcome_log.inner_sample.set(inner_samples)
            outcome_log.reagent.set(reagent_list)
            outcome_log.split_reagent.set(split_reagent_list)
            outcome_log.outcome.set(outcome_list)
        return Response([])

在这里不能用 bulk_create 因为 bulk_create 返回数据没有 pk ,需要自带 pk。暂时就这么用吧

参考: https://segmentfault.com/q/1010000015137378

相关文章

网友评论

      本文标题:2019-10-18 Django 添加包含 ManytoMan

      本文链接:https://www.haomeiwen.com/subject/hlcwmctx.html