On Wed, 2007-04-04 at 10:10 +0000, Andrew Durdin wrote: > I've got a situation where one of my django apps needs to perform some > initialisation that includes processing the models from all the > installed apps -- and it needs its own models to have been initialised > also. Because of this, I can't do my initialization when the app's > __init__ module is loaded, because it may be too soon for other models > to have been loaded, and in any case, its own models have not been > loaded. > > The solution I came up with was to hook the request_started signal, do > the initialisation then, and unhook the signal if initialisation > completed successfully. A corollary of this is that the > initialisation will only happen when running a server, and not (for > example) when running a shell, or tests, or otherwise using the django > framework. The code I'm using for this is below; I define an > initialise() function in my app's __init__ module and then call > prepare_app(initialise). > > Is there a better way to perform application-specific initialisation, > after all apps and models have been loaded, but before other > processing begins?
I cannot think of any reliable way to solve this. It's a reasonably hard problem to solve correctly, apparently. I say this because we currently are *not* solving it in django/db/models/loading.py when we try to initialise a cache of all apps and models. The difficulty is nested imports and telling when a module has been fully imported. Our current solution works most of the time, but that just makes the occasional problem case that much more intrusive. [Having mentioned loading.py, I would caution against trying to hook your processing into the initialisation of the cache. When I get a chance I'm going to declare the current approach we have there a loser and teach the cache how to handle "partially initialised" state so that it tries to fill itself in a bit more if there is a miss.] Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---