以最少数量的数据库查询进行保存(以最小化保存方法的调用)

问题描述 投票:2回答:1

我正在处理带有大量嵌套信息的json文件。为此,在UploadElementFromExcelFile(APIView)类中,我在其末尾使用了嵌套循环,称为serializer.save()方法。然后,在ElementCommonInfoSerializer方法的create()串行器中,我保存/更新从串行器接收的数据。另外,我创建/更新RfiParticipationStatus模型,该模型仅与最高嵌套级别有关(使用parent_category变量)。但是由于create方法是为循环的最低级成员调用的,因此我对RfiParticipationStatus模型进行了许多无用的数据库查询。与company_information保存相同。这是来自共享json文件的额外信息。它不涉及序列化对象,并且我只需要在post方法调用的开头保存一次company_information。在我的代码实现中,根据for循环的深度和内容,保存请求会发生数百次。

json数据样本

[
    {
        "Company_info": [
            {
                "question": "Company name",
                "answer": "Test"
            },
            {
                "question": "Parent company (if applicable)",
                "answer": "2test"
            },
            {....},
            {....}
        ]
    },
    {
        "Parent Category": " rtS2P",
        "Category": [
            {
                "Analytics": [
                    {
                        "Data Schema": [
                            {   '....': "",
                                "Attachments/Supporting Docs and Location/Link": "tui",
                                "SM score": 4,
                                "Analyst notes": "tytyt"
                            },
                            {   '....': "",
                                "Attachments/Supporting Docs and Location/Link": null,
                                "SM score": null,
                                "Analyst notes": null
                            },
                        ]
                    },
                    {
                        "Data Management": [
                            {
                                '....': "",
                                "Attachments/Supporting Docs and Location/Link": null,
                                "SM score": null,
                                "Analyst notes": null
                            },
                            {....}
                        ]
                    }
                ]
            },
            {
                "Configurability": [...]
            }
        ]
    },
    {
        "Parent Category": "DFG",
        "Category": [
            {
                "Contingent Workforce / Services Procurement": [...]
            },
            {
                "Performance Management": [...]
            }
        ]
    },
        "Parent Category": "...",
         .....
]

views.py

class UploadElementFromExcelFile(APIView):
    serializer_class = ElementCommonInfoSerializer

    def post(self, request, *args, **kwargs):
        context = {'rfiid': kwargs.get('rfiid'), 'vendor': kwargs.get('vendor'), 'analyst': kwargs.get('analyst')}
        data = request.data  # data is list of dict
        company_information = next(iter(data))
        context.update(company_information)

        try:
            with transaction.atomic():
                for pc_data in data[1:]:  # from dict get PC and Category participate data, exclude firs element - CI
                    parent_category = pc_data.get('Parent Category')
                    category_data = pc_data.get('Category')
                    for data in category_data:
                        for category, values in data.items():  # Get category name
                            for subcats in values:
                                for subcat, element_list in subcats.items():  # Get subcategory name
                                    for num, element in enumerate(element_list, 1):  # Get element info
                                        # some logic
                                        data = {......, ......,}
                                        serializer = ElementCommonInfoSerializer(data=data, context=context)
                                        serializer.is_valid(raise_exception=True)
                                        serializer.save()
        except ValidationError:
            return Response({"errors": (serializer.errors,)},
                            status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response(request.data, status=status.HTTP_200_OK)

serializer.py

class ElementCommonInfoSerializer(serializers.ModelSerializer):
    .....
    .....
    def create(self, validated_data):
        ....

        # !!!! Question is in the  rfi_part_status variable below

        rfi_part_status, _ = RfiParticipationStatus.objects.update_or_create(status=pc_status, vendor=vendor, rfi=round,
                                                                             pc=parent_category.first(),
                                                defaults={'last_vendor_response': lvr, 'last_analyst_response': lar})

        # And question else in company_information save
                company_information = self.context.get('Company_info')
        for ci in company_information:
            ciq, _ = CompanyGeneralInfoQuestion.objects.get_or_create(question=ci.get('question'), rfi=round)
            cia, _ = CompanyGeneralInfoAnswers.objects.get_or_create(vendor=vendor, question=ciq,
                                                                 answer=ci.get('answer'))

        #another crete logic
        .....
        .....

        return self

问题是,仅在通过嵌套循环的最上层元素(rfi_part_status)时,才如何调用for pc_data in data[1:]:对象的创建。和company information

的情况相同

UPD(根据Linovia问题)!models.py

class RfiParticipationStatus(models.Model):
    status = models.CharField(max_length=50, choices=STATUS_NAME)
    vendor = models.ForeignKey('Vendors', models.DO_NOTHING, related_name='to_vendor_status')
    rfi = models.ForeignKey('Rfis', models.DO_NOTHING, related_name='to_rfis_status')
    pc = models.ForeignKey(ParentCategories, models.DO_NOTHING, blank=True, null=True)
    last_vendor_response = models.IntegerField(blank=True, null=True)
    last_analyst_response = models.IntegerField(blank=True, null=True)

当创建RfiParticipationStatus对象时,仅从序列化程序数据中获取的pc(父类别)值。所有其他所有值均在该过程中计算。

python-3.x django-rest-framework django-serializer
1个回答
0
投票

您应该将业务逻辑保持在Django模型级别,并覆盖save方法,并使用super()调用您用于自定义所需行为的重写方法。

© www.soinside.com 2019 - 2024. All rights reserved.