I created some django commands so I can execute it like python manage.py my_custom_task
. That works locally. But looks like it will not work in the build server because it will require a database connection when running a django command. Can I run a python manage.py my_custom_task
that will ignore checking a database connection since my_custom_task
does not really require a DB?
Asked
Active
Viewed 45 times
0

alltej
- 6,787
- 10
- 46
- 87
-
Why wouldn't you have a db connection on your production server? Even if your management command doesn't use your models, what's the problem? – dirkgroten Sep 27 '19 at 16:51
-
This is in the build server that creates the docker image. So it is not yet in the deployed environment. The build environment will not have this DB access. – alltej Sep 27 '19 at 17:01
-
What does your script do? Does it need to be a management command (which needs to load the entire Django stack, including models etc...)? Why can't it be just a plain python script that imports just what's needed to do its job? – dirkgroten Sep 27 '19 at 17:19
-
I just tested, you can run a manage.py that doesn't do anything with your models even if there's no connection to your db (I tried `compilemessages` and it just worked). The connection is only established when needed. So if `my_custom_task` doesn't fetch model instances or something like that, you're fine. – dirkgroten Sep 27 '19 at 17:22
-
The script does is generate js/html files. It imports some django stuff like django.http, django.urls , django.template.loader, django.core.management.base, django.contrib.sites.models and django.conf. So it does not reference the db models. – alltej Sep 27 '19 at 22:49
-
Try it. Change your local settings to break the connection to your local db and check if it needs to establish a connection. Note that if it uses `sites` it would seem it needs the db. – dirkgroten Sep 28 '19 at 06:25
-
It does use the `site`. The task generates the dynamic html based on the domain. So looks like I cannot use this task. I am trying to create a docker image and this docker image will be deployed to different environments(dev, qa, staging, etc) – alltej Sep 28 '19 at 10:20