12

この関数を毎日深夜に実行して、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()
4

3 に答える 3

25

他の人が言ったように、Celery は特定の時間に実行するタスクをスケジュールできます。

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

于 2014-05-10T10:59:05.103 に答える
1

見て:

Celery - 分散タスク キュー

于 2014-05-09T11:59:43.250 に答える