Hello,
I have some server startup code that is lying in the "models.py" of one of my Django apps. I need to run that code on server startup time.
The problem is, that code issues a SQL query, which prevents me from running syncdb
with psycopg2
(it breaks the transaction, and tables are not created.)
Putting the code in a middleware and raising django.core.exceptions.MiddlewareNotUsed
is not optimal as I'd like to have the effect in the Django shell too (and putting initialization code in a middleware doesn't sound right anyway.) Also I'd need to wait for the first request to do that. I want to run code on server startup, not when the first customer comes knocking on my website.
Server startup signals are still not implemented in Django, so that's not an option.
Thus, I'd like to somehow either:
- check if Django is running a syncdb, so I don't do the queries,
- or, alternatively, check it the corresponding tables are there, and if they are not, then, too, just don't do any queries
Non of the above two options have I found in any of the documentation. How do I do that? Or is there a better (i.e. sane) way of doing what I want to do?