当DateTimeField等于当前日期和时间时,Django执行函数 [英] Django Execute Function When DateTimeField Equals Current Date and Time

查看:37
本文介绍了当DateTimeField等于当前日期和时间时,Django执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经实现了我网站的订阅产品.

So I have implemented a subscription product to my website.

当他们开始订阅时,当前日期和时间将存储到数据库中.当他们取消订阅时,开始日期会加上一段时间,因此我知道何时取消订阅并在其他位置更改某些值.

When they start the subscription, the current date and time is stored into the database. When they cancel the subscription, a period of time is added onto the start date so I know when to cancel the subscription and change some values elsewhere.

这很好,我知道如何使用Django和Python,但是,当将来的取消日期到来时,我被困在最后一点.

This is fine, I know how to use Django and Python, however, I am stuck on the last bit of, when the cancel date comes around in the future.

问题:当取消日期(在数据库中)等于当前日期和时间时,如何执行功能.

以下是我将使用的模型的简单示例:

Below is an simple example of the model I will be using:

models.py

models.py

class Subscriptions(models.Model):
    subscription_id = models.AutoField(primary_key=True)
    start_date = model.DateTimeField(auto_now_add=True)
    cancel_date = model.DateTimeField(auto_now_add=False)
    person_id = model.ForeignKey('Persons')

class Persons(models.Model):
    person_id = models.AutoField(primary_key=True)
    value_to_change = models.BooleanField()

在您问我没有尝试任何代码之前,我找不到解决此问题的方法.谢谢< 3

Before you ask I have not attempted any code as I couldn't find a solution for this problem. Thanks <3

推荐答案

没有Celery,安装在提供CRON的UNIX系统上(cron doku:例如

Without Celery, installed on UNIX system providing CRON (cron doku: e.g. https://www.computerhope.com/unix/ucrontab.htm):

  1. 编写命令 https://docs.djangoproject.com/en/2.1/howto/custom-management-commands/会获取过去使用cancel_date并且尚未取消的对象.(如果您使用datetime.now()(具有最佳粒度的非常精确的查找)进行查找,那么您将很幸运找到任何东西.)
  1. write a Command https://docs.djangoproject.com/en/2.1/howto/custom-management-commands/ that fetches the objects for which cancel_date is in the past and that have not been cancelled yet. (If you do the lookup with datetime.now() (a very precise lookup with the finest granularity), you would have to be more than lucky to find anything.)

您应该添加另一个日期字段,告诉您系统实际执行取消操作的时间,并且您应同时将cancel_date和cancelled_date都设置为空.

You should add another date field that tells you when the system actually ran the cancellation, and you should allow both the cancel_date and cancelled_date to be null.

class Subscriptions(models.Model):
    subscription_id = models.AutoField(primary_key=True)
    start_date = model.DateTimeField(auto_now_add=True)
    cancel_date = model.DateTimeField(auto_now_add=False, null=True)
    cancelled_date = model.DateTimeField(null=True)
    person_id = model.ForeignKey('Persons')

class CancellationCommand(BaseCommand):
    def handle(self, *args, **options):
        now = datetime.now()
        to_cancel_qs = Subscriptions.objects.exclude(
            cancelled_date__isnull=False).filter(
            cancel_date__lte=now)
        for sub in to_cancel_qs.all():
            # do your cancelling
            sub.cancelled_date = now
            sub.save()
        # or: to_cancel_qs.update(cancelled_date=now)

  1. 安装一个可通过./manage.py your_command定期运行此命令的cron作业

这篇关于当DateTimeField等于当前日期和时间时,Django执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆