Kenzo_Gilead
Kenzo_Gilead

Reputation: 2439

How to Running external command (Django) for overriding files in server?

After modified inspectdb, I want to run it when index page on web is loaded. So, in my view.py - def index, I´m trying to do the next:

def index(request):
 subprocess.Popen("rm /path/app/models.py", shell=True)
 subprocess.Popen("python2.7 /path/manage.py inspectdb_New > /path/app/models.py", shell=True)
 return render_to_response('index/index.html', context_instance = RequestContext(request))

That is not working. I tried with os.system, subprocess.call as well, but it´s still not working. For me, at least, looks like I can´t modify models.py in execution time but I don´t know what could be the problem...

Any idea guys?

Thanks.

Upvotes: 1

Views: 157

Answers (2)

peroksid
peroksid

Reputation: 927

There are 2 parts in you question: a) you want the shell to do something when view function is called. I have alarm beeping already. b) you want to load new model. It won't gonna happen. You must reload to have new code loaded in (model introspected, etc.). Probably you use runserver command, take a look on Django autoreloader code: https://github.com/django/django/blob/master/django/utils/autoreload.py

It tracks files listed by gen_filenames() and reload when something was changed. You model was not listed so the change to the code will not be reflected.

I would touch something or remove .pyc files to force the reloader.

Upvotes: 1

ilse2005
ilse2005

Reputation: 11439

You can't change your models.py while the webserver is running, because models (and other python code) are only loaded at server startup.

Upvotes: 1

Related Questions