gpt4 book ai didi

Django - 如何每天运行一个函数?

转载 作者:行者123 更新时间:2023-12-04 10:07:27 24 4
gpt4 key购买 nike

我想每天午夜运行这个函数来检查expiry_date_notification。我能做什么?我是 Django 和 python 的新手。

def check_expiry_date(request):
products = Product.objects.all()
for product in products:
product_id = product.id
expiry_date = product.expiry_date
notification_days = product.notification_days
check_date = int((expiry_date - datetime.datetime.today()).days)
if notification_days <= check_date:
notification = Notification(product_id=product_id)
notification.save()

最佳答案

正如其他人所说,Celery can schedule tasks to execute at a specific time .

from celery.schedules import crontab
from celery.task import periodic_task

@periodic_task(run_every=crontab(hour=7, minute=30, day_of_week="mon"))
def every_monday_morning():
print("This is run every Monday morning at 7:30")

通过 pip install django-celery 安装

关于Django - 如何每天运行一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23563934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com