8

I need a place to run an initialization code that is application specific (like connecting to signals). When I put the code to __init__.py module of an application I've ended up with a circular import of the models.

Is there a way to fire a function when the framework is setup and before any request is executed?

I use quite old version of django 96.6, but I'm also interested in the solutions for the current version.

Regarding the duplication of other questions: Here is how the question differ from the duplicates suggested by S.Lott in comments:

Comments to current solutions: I can't use urls as most of my apps don't have any urls exposed. They just listen to signals and store additional information in the database.

Community
  • 1
  • 1
Piotr Czapla
  • 25,734
  • 24
  • 99
  • 122
  • Duplicate: http://stackoverflow.com/questions/1797046/correct-place-to-put-extra-startup-code-in-django, Duplicate: http://stackoverflow.com/questions/1986060/where-should-i-place-the-one-time-operation-operation-in-the-django-framework – S.Lott Jan 19 '10 at 16:49
  • 1
    @S.Lott It is not a duplicated. I need a code that is ran for each instance of the python interpreter right after the django framework is initialized. I will elaborate in the question – Piotr Czapla Jan 19 '10 at 17:00

4 Answers4

6

Signals, specifically, are recommended to be put in the models.py of your app.

Try models.py or urls.py and let us know if you have any luck.

Ben Edwards
  • 502
  • 2
  • 4
  • I can't put it in to the models as I need the function in its own module and I need to use my models in that module. – Piotr Czapla Jan 19 '10 at 17:18
  • Your signals function definition can be in a seperate file (I use signals.py -- creative, I know) but then in my models.py I add at the bottom something like post_save.connect(profile_updater, sender=MyProfile) of course with the proper imports. – ashchristopher Jan 19 '10 at 18:43
  • 1
    @ashchristopher but if your signals.py use models you end up with circular references. I don't want to import models in very signal handler. – Piotr Czapla Jan 20 '10 at 09:03
  • 1
    Ah - I understand. I dealt with this by including the import within the signal function definition. – ashchristopher Jan 21 '10 at 19:31
  • I do want to note that it was possible for me to put even the connect statements into the other module. The main restriction I found was that the methods called had to be directly accessible. If the post_save_user func and the connect are both in another function, it will not get called. But if the func is directly on the module and the connect statements are in a method, then it will work. So at the bottom of models.py I do "import foo" then "foo.connect()". – ShawnFumo Aug 23 '13 at 17:14
5

The best place for stuff like this... anywhere, just import it in your urls.py file (for obvious reasons urls are loading before any requests).

Alex Gaynor
  • 14,353
  • 9
  • 63
  • 113
3

If you don't provide urls, then you really need to put it in models.py, that's just the way it is.

Now, on to your problems: You want to define it in its own module, great, do that. To avoid a circular import, use django.db.models.get_model to return the model dynamically for you. You can provide an initialisation function for your signals module to import the relevant model and connect the relevant signals. This function would then be called at the end of models.py, being run only ever once and after your model is initialised.

There's still a chance that this wont work (if the models aren't yet ready when you set it up), but give it a try and let us know.

Will Hardy
  • 14,588
  • 5
  • 44
  • 43
0

For me, the following works:

In init.py:

from . import models
from . import signals

signals.py imports from models, but not vice versa. signals.py contains module code that is run immediately when it is imported and is thus run when the django server starts.

Risadinha
  • 16,058
  • 2
  • 88
  • 91