'django cron job cannot get model query
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
Solution 1:[1]
I struggled with this all day today and for me the problem was that Django wasn't connecting to the database through cron so it was defaulting to the sqlite database which was not migrated. The reason for this is that cron sets up a minimalistic environment and doesn't read the environment variables that you may have already had set. Most people using Django will setup their databases conditionally based on environment variables.
I solved the issue by giving cron access to the 'DATABASE_URL' environment variable that I needed. I found two ways of doing so. One way is to export the env variables that you need to the global /etc/environment file:
env | grep DATABASE_URL >> /etc/environment
You can do this from your entrypoint.sh script. I tried this out myself and it worked for me.
The other solution that I found seems a little bit more complex. It's described here: https://roboslang.blog/post/2017-12-06-cron-docker/. I haven't tried it, but it looks like it should work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dre |
