Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django app initalization code (like connecting to signals)

Tags:

python

django

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:

  • Correct place to put extra startup code in django? Django need to be fully initialized when the function is ran. So code in manage.py won't work.

  • Where should I place the one-time operation operation in the Django framework? The function initialize the connection between my applications. So the code must be ran in each thread that will actually handle the requests.

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.

like image 995
Piotr Czapla Avatar asked Jan 19 '10 16:01

Piotr Czapla


2 Answers

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.

like image 150
Ben Edwards Avatar answered Sep 26 '22 00:09

Ben Edwards


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).

like image 28
Alex Gaynor Avatar answered Sep 25 '22 00:09

Alex Gaynor