是否可以在特定日期自动运行 django 函数?
在我的 django 项目中,如果数据库中的某些条目太旧,我希望能够自动删除它们。我可以编写一个函数来检查创建日期,如果它太旧,则将其删除,但我希望该函数定期自动运行。是否可以这样做? 谢谢
In my django project I would like to be able to delete certain entries in the database automatically if they're too old. I can write a function that checks the creation_date and if its too old, deletes it, but I want this function to be run automatically at regular intervals..Is it possible to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是
cron
的用途。This is what
cron
is for.您最好阅读 Django 文档的这一部分 http://docs.djangoproject.com/en/1.2/howto/custom-management-commands/#howto-custom-management-commands
然后你可以将你的函数创建为 Django 管理命令并将其与 *nix 上的 cron(或 Windows 上的计划任务)结合使用,以按计划运行它。
有关 cron 的良好介绍指南,请参阅此 http://www.unixgeeks .org/security/newbie/unix/cron-1.html
You will be better off reading this section of the Django docs http://docs.djangoproject.com/en/1.2/howto/custom-management-commands/#howto-custom-management-commands
Then you can create your function as a Django management command and use it in conjunction with cron on *nix (or scheduled tasks on Windows) to run it on a schedule.
See this for a good intro guide to cron http://www.unixgeeks.org/security/newbie/unix/cron-1.html
你需要的是一个 cron 作业。
cron 作业是基于时间的作业调度程序。大多数网络托管公司都提供此功能,让您可以在选择的时间运行服务或脚本。大多数基于 Unix 的操作系统都具有此功能。
如果您在 stackoverflow 的姊妹网站 serverfault.com 上提出这个问题,您会得到更好的直接帮助。
What you require is a cron job.
A cron job is time-based job scheduler. Most web hosting companies provide this feature which lets u run a service or a script at a time of your choosing. Most Unix-based OSes have this feature.
You would have better direct help asking this question on serverfault.com, the sister site of stackoverflow.