Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running startup code right after Django settings? (also for commands)

I am using mongoengine and would like to run connect() after settings (not inside them as suggested in its docs). This is actually more like a general question how to run code right after all settings are loaded.

Update: I need a solution for management commands as well. Common approach is adding a middleware with exception MiddlewareNotUsed or adding code to root urls.py, but both don't work for commands.

like image 653
Andrei Avatar asked Feb 13 '12 11:02

Andrei


1 Answers

The normal place for startup-like code is in a urls.py (when you need the settings to be already loaded). Django doesn't have a good spot yet for this.

(There is an "app refactor" branch that a gsoc student worked on in 2011, but it didn't get merged into core django yet. This "app refactor" includes a solution to your very problem, but that doesn't help you...)

You mention that a management command also needs it. Is that your own management command? Nothing stops you from importing the urls.py there, is it?

This is sadly one of the few Django weak points. Luckily there aren't that many :-)

like image 141
Reinout van Rees Avatar answered Oct 11 '22 12:10

Reinout van Rees