settings set in stone on import?

31 views
Skip to first unread message

Tin Tvrtković

unread,
Mar 27, 2014, 5:45:51 PM3/27/14
to django...@googlegroups.com
Hi,

I'm trying to create an init script for a project of mine (open source). In short, my users should be able to initialize a Django project from scratch with one command. The internal workflow of the command would go something like this:

* call startproject with a custom template
* call syncdb --noinput
* call collectstatic --noinput
* ...

The thing is, to call the startproject management command I need to indirectly import django settings, which are unset at that time. After that's done and the proper settings.py gets rendered to the filesystem, I was hoping to use those settings to call manage.py commands (syncdb and collectstatic obviously need a working, valid settings.py), but it would appear Django has already initialized itself into "no settings mode". I was also hoping to avoid doing this out of process.

Is there a way to make Django reread DJANGO_SETTINGS_MODULE after it has been imported? Also, it would seem django.core.management.call_command doesn't take a 'settings' keyword argument, like django-admin.py does.

Cheers,
Tin

Erik Cederstrand

unread,
Mar 28, 2014, 9:43:56 AM3/28/14
to Django Users
Hello Tin,

Den 27/03/2014 kl. 22.45 skrev Tin Tvrtković <tinch...@gmail.com>:

> Is there a way to make Django reread DJANGO_SETTINGS_MODULE after it has been imported? Also, it would seem django.core.management.call_command doesn't take a 'settings' keyword argument, like django-admin.py does.

The standard Python built-in function reload() http://docs.python.org/2.7/library/functions.html#reload should be able to do this.

Erik

Tin Tvrtković

unread,
Mar 28, 2014, 8:31:07 PM3/28/14
to django...@googlegroups.com
Hi Erik,

hey, that worked! I had to reload both the django.conf and django.core.management modules, but after doing that calling Django management commands that depend on valid settings seems to work just fine. Thank you for the suggestion.

Before you replied I was contemplating just calling the call_command('startproject', ...) function in another process using multiprocessing. Maybe I'll try that just to see if it'd work, but I'll be sticking with reloading.

Cheers!
Reply all
Reply to author
Forward
0 new messages