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.
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 :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With