每天将字段重置为“ 0”

时间:2019-12-03 04:38:13

标签: python django django-models

我需要每天的00:00 am,将studied_today字段重设为0

原始代码: models.py

from django.db import models

from django.conf import settings

class Studied(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING)
    studied_today = models.IntegerField(default=0)

    def __str__(self):
        return self.collection.title

我尝试:

$ pip install schedule

之后,我更改原始代码:

from django.db import models

from django.conf import settings

import schedule

class Subscription(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING)
    studied_today = models.IntegerField(default=0)

    def __str__(self):
        return self.collection.title

    def job():
        studied_today = 0

    schedule.every().day.at("00:00").do(job)

我做错了什么,我该怎么做?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

根据文档https://pypi.org/project/schedule/,您需要保持schedule.run_pending()每秒运行一次,我认为更好的解决方案是在服务器上设置一个简单的crontab并在深夜触发。