在djang的jsonfield上执行任务的有效方法

2024-07-01 07:42:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在django的一个jsonfield上提取一些数据,但是我编写的queryset要花很长时间才能完成行动。行动正在查询的数据库有超过1000万条记录:(他们能用一种更有效的方法吗? 这是查询

from django.contrib.postgres.fields import JSONField
from maidea.apps.upload_service.models import MobileUploadHouseholdLog

rejected_hh_somalia = MobileUploadHouseholdLog.objects.filter(status=4, household__contains={'info':{'location':{'label':'Hantiwadag'}}})

我的型号.py你知道吗

class MobileUploadHouseholdLog(MAIDEAModel):
    objects = MobileUploadHouseholdLogManager()

    UNPROCESSED = 0
    IMPORTED_TO_SCOPE = 1
    ERROR = 2
    DUPLICATE = 3
    DISCARDED = 4

    IMPORT_STATUS = (
        (UNPROCESSED, _('New')),
        (IMPORTED_TO_SCOPE, _('Import successful')),
        (ERROR, _('Import failed')),
        (DUPLICATE, _('Duplicate upload')),
        (DISCARDED, _('Discarded upload')),
    )

    uuid = UUIDField(verbose_name=_("Unique ID"), auto=True, version=4, help_text=_('unique id'))
    household = JSONField(_("JSON for a single household"), help_text=_('Contains data for a single Household'),
                          blank=True, null=False)
    status = models.IntegerField(_("Processing status"), choices=IMPORT_STATUS)
    mobile_upload_log = models.ForeignKey(MobileUploadLog,
                                          verbose_name=_('Upload session where the Household is coming from'),
                                          related_name='mobile_upload_logs')
    processed_datetime = models.DateTimeField(null=True, blank=True,
                                              verbose_name=_('Timestamp when household was processed'))
    error = models.TextField(_("Error trace for this import"), help_text=_('Contains the exception of why this household cannot be imported'),
                             blank=True, null=False)

    def save_error(self, error):
        self.status = MobileUploadHouseholdLog.ERROR
        self.error = error
        self.processed_datetime = timezone.now()
        self.save()

    def save_imported(self):
        self.status = MobileUploadHouseholdLog.IMPORTED_TO_SCOPE
        self.processed_datetime = timezone.now()
        self.save()

    def save_duplicate(self):
        self.status = MobileUploadHouseholdLog.DUPLICATE
        self.processed_datetime = timezone.now()
        self.save()

任何洞察都会谢谢。谢谢你知道吗


Tags: namefromimportselftruedatetimemodelssave

热门问题