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?
CodePudding user response:
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
CodePudding user response:
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)