Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically disable DEBUG in Django command

I am using a custom Django command to generate XML sitemaps for a site with about 3-4 million data entries (./manage.py generate_sitemaps). This seems to work, but eats way too much memory when DEBUG is enabled in settings.py.

I usually have the DEBUG option enabled during development and frequently forget to disable it before starting the sitemap creation. If that happens, the memory starts filling up until the script crashes after about 2-3 hours. Very annoying.

Is there a way to temporarily disable the debug setting for the execution of a Django command? I thought of importing the settings module and overriding the option, but I don't think that'll work.

like image 522
Danilo Bargen Avatar asked Oct 16 '25 14:10

Danilo Bargen


2 Answers

I think you have a couple of options here:

  1. Import settings and throw an error to remind yourself to turn debug off.

  2. Use the --settings= and set that equal to a file (e.g. gen_settings.py) file specifically for your generate_sitemaps command where DEBUG=False. Then create an alias for ./manage.py generate_sitemaps --settings=gen_settings

http://docs.djangoproject.com/en/dev/topics/settings/ warns specifically to not change the settings at runtime

I've used the second option before and it worked fairly well. Better than being annoyed after 2-3 hours =)

like image 152
dting Avatar answered Oct 19 '25 03:10

dting


I am not really sure it helps you, but you can try:

from django.conf import settings

tmp = settings.DEBUG
settings.DEBUG = False
# some your actions
# ...
# ...
settings.DEBUG = tmp

Alternatively you can use separated settings file and set it in command line like

./manage.py your_command --settings=another_settings.py

And in that another_settings.py:

from .settings import *
DEBUG = False
like image 34
Millioner Avatar answered Oct 19 '25 03:10

Millioner