在特定日期后删除Django模型实例的最佳方法 [英] Best way to delete a django model instance after a certain date

查看:55
本文介绍了在特定日期后删除Django模型实例的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小应用程序,用户可以在其中创建事件并指定事件发生的日期。活动日期过去之后,我想删除该活动实例。我当前的尝试是抛出一个函数来检查事件是否应该在事件页面视图中过期。我不确定expiration_check函数是否以正确的方式进行检查,也不确定在视图中仅具有某个功能是否会起作用。

I am writing a little app where the user creates an event and specifies the date that event will occur. After the event date has past, I want to delete that event instance. My current attempt is throwing a function that checks if the event should expire in the event page view. I am not sure whether the expiration_check function is checking in a correct way, nor am I sure whether just having a function in the view will event work.

这是我的视图和到期函数:

Here is my view and expire function:

def event_page(request, name):
    event = Event.objects.get(name=name)

    check_expiration(event)

    if request.method == "POST":
        form = GuestForm(request.POST)
        if form.is_valid():
            Guest = form.save(commit=False)
            Guest.event = event
            Guest.save()
            return redirect(event)
    else:
        form = GuestForm()
        return render(request, "event_page.html", {"form": form, "event": event, })


def check_expiration(event):
    now = datetime.datetime.now()

    if event.date < now: #if the event date has past
        event.delete()

我收集了从用户获取日期并将其存储在DateTime字段中:date = models.DateField()

I collect the date from the user and store it in a DateTime filed: date = models.DateField()

让我知道是否需要其他详细信息。谢谢您的见解!

Let me know if any further details are needed. Any insight is appreciated, thanks!

推荐答案

如果您要在UNIX平台(GNU / Linux,OSX, ),最好使用 cron (用于定期运行事物的通用系统实用程序)。

If you're hosting your application on a UNIX platform (GNU/Linux, OSX, etc.), it's probably best to make use of cron, the generic system utility for running things periodically.

这需要将您的到期代码实施为自定义管理命令

This requires implementing your expiry code as a custom management command:


  1. 如果您还没有任何自定义管理命令,请创建以下目录结构:

  1. If you don't have any custom management commands already, create the following directory structure:

yourapp/
  management/
     __init__.py (blank)
     commands/
       __init__.py (blank)
       expire_events.py


  • expire_events中.py ,根据以下内容创建一个新类:

  • In expire_events.py, create a new class along the lines of the following:

    from django.core.management.base import NoArgsCommand
    
    class Command(NoArgsCommand):
    
        help = 'Expires event objects which are out-of-date'
    
        def handle_noargs(self):
            print Event.objects.filter(date__lt=datetime.datetime.now()).delete()
    


  • 现在您应该是能够运行 ./ manage.py expire_events ,并删除了任何具有过期日期的事件。

  • Now you should be able to run ./manage.py expire_events and have any events with expiry dates in the past deleted.

    要使用 cron 定期运行此命令(这些说明适用于GNU / Linux,但可能适用于其他UNIX变体),运行 sudo crontab -e 并添加以下行:

    To run this at regular intervals using cron (these instructions are for GNU/Linux but may well work on other UNIX variants), run sudo crontab -e and add the following line:

    */5 * * * * /path/to/your/django/app/manage.py expire_events
    

    (这将每5分钟运行一次任务;请参见 crontab文档,以获取有关指定作业运行时间的建议)

    (this would run the task every 5 minutes; see the crontab documentation for advice on specifying job run times)

    这篇关于在特定日期后删除Django模型实例的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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