[Answer]-How can I seperate the objects in database and run the code by day?

1đź‘Ť

âś…

Try Crontab: django-crontab github.
It is a django app that uses linux’s crontab to do scheduled jobs.

You may want to add a new column in you database as “processed”,

and add a scheduled job as in the Example, set the run time like this:

CRONJOBS = [
    ('0 20 * * *', 'myproject.myapp.cron.my_scheduled_job'),
]

and

python manage.py crontab add

by doing this, your code will run at 20:00 daily.

and in your cron.py

def my_scheduled_job():
    p = Place.objects.filter(processed=False)[:5]
    p.do_something()

to get the first 5 unprocessed elements and process them.

👤Tianqi Tong

Leave a comment