简体   繁体   English

如何在Django数据库中自动保存和删除数据?

[英]How to save and delete data automatically in Django database?

I am trying to build a website using Django, but I would like to update my database for every 15 mins with new scraped data.我正在尝试使用 Django 构建一个网站,但我想每 15 分钟用新的抓取数据更新我的数据库。 Also, I would like to delete the old data that has been saved for longer than one week.另外,我想删除保存超过一周的旧数据。

I was able to do it manually by using Django manage.py shell , but how can I make it automatically?我可以使用 Django manage.py shell手动完成,但如何自动完成?

Is there any library for automatic database management that is compatible with Django?是否有与 Django 兼容的自动数据库管理库?

Thank you in advance!先感谢您!

Having just implemented something similar myself, I'd have to agree with the comments in saying that this is a relatively involved process.我自己刚刚实现了类似的东西,我不得不同意评论说这是一个相对复杂的过程。 Nevertheless, it is quite doable, and there is a ton of info out there on the particulars.尽管如此,这是非常可行的,并且有大量关于细节的信息。 I'll give you an overview of what I did.我会给你一个我所做的概述。

My goal was to automatically run a function in my Django app every weekday at 16:00.我的目标是在每个工作日的 16:00 在我的 Django 应用程序中自动运行一个函数。

A quick search will tell you that you want to set up a periodic task schedule.快速搜索将告诉您要设置定期任务计划。 For that I needed:为此,我需要:

  • Celery - A popular task queue/scheduling system with Django compatibility Celery - 与 Django 兼容的流行任务队列/调度系统
  • Redis - The broker I opted to use Redis - 我选择使用的代理
  • Django Celery Beat - Celery Beat is the periodic task scheduling part of Celery - this Django package adds database integrations so that you can control the schedule of your events from the admin panel Django Celery Beat - Celery Beat 是 Celery 的周期性任务调度部分 - 这个 Django 包添加了数据库集成,以便您可以从管理面板控制事件的时间表

A setup using these 3 elements will provide everything you need to set up what you're looking for.使用这 3 个元素的设置将提供设置所需内容所需的一切。 In terms of implementation, I could try and type it all out here, but I feel I would just be copying the guides I followed.在实施方面,我可以尝试在这里全部输入,但我觉得我只是复制我遵循的指南。 I found this one particularly useful, but I assume you're not using Docker (I recommend you do though!).我发现这个特别有用,但我假设您没有使用 Docker(不过我建议您使用!)。 Either way, between that and the official Celery Getting Started Guide and Django Celery Beat guide , you'll have everything you need.无论哪种方式,在官方 Celery 入门指南和 Django Celery Beat 指南之间,您都将拥有所需的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM