在 django 中使用 celery 安排状态更改
在我的程序中,我安排了一项任务,旨在如果到期日等于我运行程序时的当前日期,则将保险状态更改为过期。该脚本假设在整个合约表中循环并获取与我们在脚本中使用的条件相对应的行。但是,它正在更改整个表的状态,包括不应受强加条件影响的行。 以下是 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()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该更新
contractList
,因为这是一个包含所有 记录的查询集,您可以使用以下内容更新该项目:您也不应该不
打破循环。
根据
get_NbDays
的实现,您还可以将其移至.filter(...)
子句,在这种情况下使用.update(...)< /代码>
You should not update the
contractList
, since that is a queryset with all records, you update that item with: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(…)