每天增加Django数据库值

发布于 2025-02-04 10:51:44 字数 65 浏览 2 评论 0 原文

假设我已经创建了一个具有数量数的DJANGO模型,每个记录的初始值等于0。我如何每天一次的每一个DB记录来递增此值?

let's assume I've created a Django model with an integer field of count, with initial value for every record equal to 0. How do I increment this value, for every record of the db, once every day?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薆情海 2025-02-11 10:51:44

您可以设置芹菜,以便每天定期运行任务。
https://docs.celereryq.dev/en/stable/stable/userguide/定期任务。html

在任务中,获取数据库记录并进行更新。

您可以检查以下信息以获取更多信息:

You can setup Celery in order to run tasks periodically, every day.
https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html

Inside the task, get the DB record and update it.

You can check the following for more information:
https://medium.com/@yedjoe/celery-4-periodic-task-in-django-9f6b5a8c21c7

氛圍 2025-02-11 10:51:44

简单的技巧
在表中添加create_on(日期)。像这样的 create_on = models.dateTimeField(auto_now_add = true)每当您创建新记录时。
每当您想查看UI的计数时,只需获取当前日期并执行一些数学方程式,这将使您之间的差异在两个日期之间,最终将是您的计数,您可以进行简单的更新查询以维持数据库中的数量。

from datetime import date

d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
print(delta.days)

simple trick
add create_on (date) in the table. like this created_on = models.DateTimeField(auto_now_add=True) whenever you create a new record.
whenever you want to check out count on UI just fetch current date and do some mathematics equation which will get you diff between two dates which ultimately will be your count and you can make simple update query to maintain count in database as well.

from datetime import date

d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
print(delta.days)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文