【问题标题】:Saving with minimum number of database queries (minimizing save method call)以最少数量的数据库查询保存(最小化保存方法调用)
【发布时间】:2020-05-05 21:39:33
【问题描述】:

我正在处理一个包含大量嵌套信息的 json 文件。为此,在UploadElementFromExcelFile(APIView) 类中,我使用嵌套循环,在其最后我调用serializer.save() 方法。然后在create() 方法中的ElementCommonInfoSerializer 序列化程序中,我保存/更新从序列化程序接收到的数据。此外,我创建/更新了一个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

问题是我如何仅在通过嵌套循环的最上面的元素 (for pc_data in data[1:]:) 时调用 rfi_part_status 对象的创建。和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(父类别)值取自序列化器数据。在此过程中计算所有其他值。

【问题讨论】:

  • 不幸的是,这个问题有点太模糊了。我们不知道需要验证什么才能获得RfiParticipationStatus 对象,
  • @Linovia 我已经更新了我的问题,希望它能澄清情况。

标签: python-3.x django-rest-framework django-serializer


【解决方案1】:

您应该将业务逻辑保留在 Django 模型级别并覆盖 save 方法并使用 super() 调用您用于客户所需行为的覆盖方法。

【讨论】:

  • 感谢您的反馈,您能写一个更详细的答案吗?如果我需要补充更多信息,你直接说吧。
猜你喜欢
  • 2013-01-08
  • 1970-01-01
  • 2014-07-26
  • 2012-06-02
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
相关资源
最近更新 更多