在 django 中使用 celery 安排状态更改

发布于 2025-01-11 10:14:30 字数 1589 浏览 0 评论 0原文

在我的程序中,我安排了一项任务,旨在如果到期日等于我运行程序时的当前日期,则将保险状态更改为过期。该脚本假设在整个合约表中循环并获取与我们在脚本中使用的条件相对应的行。但是,它正在更改整个表的状态,包括不应受强加条件影响的行。 以下是 jobs.py 文件,该文件计划在条件为真时将保险状态更改为 Expiré 的任务。

from assurance_auto.models import Contrat
from datetime import datetime, timedelta,date
from django.conf import settings

def status_schedule(): 
    
    contractList = Contrat.objects.all()
    for contrat in contractList:
        if contrat.get_NbDays()<=date.today() and contrat.statut_assurance=='Encours':
            contractList.update(statut_assurance='Expiré')
            print('Numéro de contrat est :',contrat.numero_de_contrat,\
                ' et le statut est: ',contrat.statut_assurance)
        else:
            break

下面是 updater.py 文件。该函数是关于调度作业的执行时间

from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from .jobs import status_schedule
def start():
    scheduler = BackgroundScheduler()
    scheduler.add_job(status_schedule, 'interval', seconds=5)
    scheduler.start()

在 apps.py 文件中,我有下面的程序,其中包括在程序运行后运行更新程序文件。

from django.apps import AppConfig
class AssuranceAutoConfig(AppConfig):
    name = 'assurance_auto'
    def ready(self):
        from jobs import updater
        updater.start()

截图发生的错误圈出的两行是应该将状态更改为过期的行。但是,所有行的状态都更改为 Expiré,这不是我想要的结果。 请问这个编程错误该如何解决?

In my program, I have scheduled a task that is aimed at changing the status of insurance to Expiré if the due date is equal to the current date when I run the program. The script supposes to loop up throughout the Contract's table and fetches the rows that are corresponding to the condition we used in the script. However, it is changing the status of the whole table including rows that should not be affected by the imposed condition.
Here is the jobs.py file that is scheduling the task of changing the status of the insurance to Expiré if the condition is true.

from assurance_auto.models import Contrat
from datetime import datetime, timedelta,date
from django.conf import settings

def status_schedule(): 
    
    contractList = Contrat.objects.all()
    for contrat in contractList:
        if contrat.get_NbDays()<=date.today() and contrat.statut_assurance=='Encours':
            contractList.update(statut_assurance='Expiré')
            print('Numéro de contrat est :',contrat.numero_de_contrat,\
                ' et le statut est: ',contrat.statut_assurance)
        else:
            break

Below is the updater.py file. This function is about scheduling the execution time of the job

from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from .jobs import status_schedule
def start():
    scheduler = BackgroundScheduler()
    scheduler.add_job(status_schedule, 'interval', seconds=5)
    scheduler.start()

In the apps.py file, I have the program below which consists of running the updater file once the program is running.

from django.apps import AppConfig
class AssuranceAutoConfig(AppConfig):
    name = 'assurance_auto'
    def ready(self):
        from jobs import updater
        updater.start()

Screenshot of the occurring error
The two circled rows are the ones supposed to change the status to Expiré. However, all rows are changing the status to Expiré which is not what I want to get as a result.
How can I solve this programming error, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁憨憨 2025-01-18 10:14:30
contractList = Contrat.objects.all()
for contrat in contractList:
    if contrat.NbDays<=date.today() and contrat.statut_assurance=='Encours':
        Contrat.objects.filter(id=contract.id).update(statut_assurance='Expiré')
        print('Numéro de contrat est :',contrat.numero_de_contrat,\
            ' et le statut est: ',contrat.statut_assurance)
contractList = Contrat.objects.all()
for contrat in contractList:
    if contrat.NbDays<=date.today() and contrat.statut_assurance=='Encours':
        Contrat.objects.filter(id=contract.id).update(statut_assurance='Expiré')
        print('Numéro de contrat est :',contrat.numero_de_contrat,\
            ' et le statut est: ',contrat.statut_assurance)
◇流星雨 2025-01-18 10:14:30

您不应该更新 contractList,因为这是一个包含所有 记录的查询集,您可以使用以下内容更新该项目:

def status_schedule(): 
    for contrat in Contrat.objects.filter(statut_assurance='Encours'):
        if contrat.get_NbDays() <= date.today():
            contrat.statut_assurance = 'Expiré'
            contrat.save()
            print('Numéro de contrat est :',contrat.numero_de_contrat,\
                ' et le statut est: ',contrat.statut_assurance)

您也不应该 打破循环。

根据 get_NbDays 的实现,您还可以将其移至 .filter(...) 子句,在这种情况下使用 .update(...)< /代码>

You should not update the contractList, since that is a queryset with all records, you update that item with:

def status_schedule(): 
    for contrat in Contrat.objects.filter(statut_assurance='Encours'):
        if contrat.get_NbDays() <= date.today():
            contrat.statut_assurance = 'Expiré'
            contrat.save()
            print('Numéro de contrat est :',contrat.numero_de_contrat,\
                ' et le statut est: ',contrat.statut_assurance)

You should also not break the loop.

Depending on the implementation of get_NbDays, you can also move that to the .filter(…) clause and in that case work with a .update(…)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文