I am using django-crontab and the following cron job is working well:
the following cron job is added by
python manage.py crontab add
settings.py
CRONTAB_COMMAND_SUFFIX = '2>&1'
CRONJOBS = [
('*/1 * * * *', 'my_app.cron.test','>> ~/cron_job.log'),
]
my_app/cron.py
from datetime import datetime
def test():
print('HELLO : {}'.format(datetime.now()))
and once the server runs, it prints out to the log file:
~/cron_job.log
>...
>HELLO : 2018-01-04 23:52:02.983604
>...
same thing if I want to add a query for all my models :
my_app/cron.py
from datetime import datetime
from django.apps import apps
def test():
print('HELLO : {}'.format(datetime.now()))
print(apps.get_models())
~/cron_job.log
>...
>HELLO : 2018-01-05 10:00:02.283938
[<class 'django.contrib.admin.models.LogEntry'>, <class 'django.contrib.auth.models.Permission'>, <class 'django.contrib.auth.models.Group'>, <class 'django.contrib.auth.models.User'>, <class 'django.contrib.contenttypes.models.ContentType'>, <class 'django.contrib.sessions.models.Session'>, <class 'my_app.models.UserProfile'>, <class 'my_app.models.Post'>, <class 'my_app.models.Comment'>, ...]
>...
But when I start to query my model entries:
my_app/cron.py
from datetime import datetime
import blog_app.models
def test():
print('HELLO : {}'.format(datetime.now()))
for post in my_app.models.Post.objects.all():
print(post.title)
nothing is printed out. The model entries exist though.Any idea?
>...
>django.db.utils.OperationalError: no such table: blog_app_post