views:

48

answers:

3

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?

+1  A: 

Django should also load urls.py on startup; I cannot tell you if that's a suitable place for your code, but give it a try if you want to!

lazerscience
That sounds even weirder,though it might solve the actual problem.
Attila Oláh
Well I also think it's weird, but using `urls.py` sometimes seems to be a useful workaround for similar problems!
lazerscience
Well, as nobody seems to be able to come up with a better idea, I'm accepting your solution as a workaround. Thanks for the help.
Attila Oláh
+1  A: 

Have you considered connecting to the post_syncdb signal? Perhaps you can run the custom SQL on receiving this signal. This may solve on part of your problem; you still have to figure out how to run the SQL on server startup.

Manoj Govindan
The thing is, I don't need to do anything on the post_syncdb, as I'm only doing read-only queries (i.e. `SELECT`). That is, I'm caching some information from the database to memory. Perhaps a lazy object could do the job though;
Attila Oláh
A: 

You can put a check in settings.py:

import sys
...
IN_SYNCDB = ('syncdb' in sys.argv)

Then

if not settings.IN_SYNCDB:
    # run SQL code
Jordan Reiter
from django.core import management; management.run_command('syncdb'); this will not work.
Attila Oláh