在特定日期后删除Django模型实例的最佳方法

编程入门 行业动态 更新时间:2024-10-09 20:26:06
本文介绍了在特定日期后删除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:

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

  • 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模型实例的最佳方法

    本文发布于:2023-11-24 12:07:34,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1625139.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:实例   模型   日期   方法   在特定

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!