14đź‘Ť
In Django 1.7 Apps can implement the ready() method: https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready
7đź‘Ť
My question is a more poorly phrased duplicate of this question: Where To Put Django Startup Code. The answer comes from that question:
Write middleware that does this in init and afterwards raise django.core.exceptions.MiddlewareNotUsed from the init, django will remove it for all requests…
See the Django documentation on writing your own middleware.
- [Django]-Django template, if tag based on current URL value
- [Django]-Django & Redis: How do I properly use connection pooling?
- [Django]-'staticfiles' is not a valid tag library: Template library staticfiles not found
5đź‘Ť
I had to do the following monkey patching. I use django 1.5 from github branch. I don’t know if that’s the proper way to do it, but it works for me.
I couldn’t use middleware, because i also wanted the manage.py scripts to be affected.
anyway, here’s this rather simple patch:
import django
from django.db.models.loading import AppCache
django_apps_loaded = django.dispatch.Signal()
def populate_with_signal(cls):
ret = cls._populate_orig()
if cls.app_cache_ready():
if not hasattr(cls, '__signal_sent'):
cls.__signal_sent = True
django_apps_loaded.send(sender=None)
return ret
if not hasattr(AppCache, '_populate_orig'):
AppCache._populate_orig = AppCache._populate
AppCache._populate = populate_with_signal
and then you could use this signal like any other:
def django_apps_loaded_receiver(sender, *args, **kwargs):
# put your code here.
django_apps_loaded.connect(django_apps_loaded_receiver)
- [Django]-Django date query from newest to oldest
- [Django]-'WSGIRequest' object has no attribute 'user' Django admin
- [Django]-Docker&Celery – ERROR: Pidfile (celerybeat.pid) already exists
3đź‘Ť
As far as I know there’s no such thing as “fully loaded”. Plenty of Django functions include import something
right in the function. Those imports will only happen if you actually invoke that function. The only way to do what you want would be to explicitly import the things you want to patch (which you should be able to do anywhere) and then patch them. Thereafter any other imports will re-use them.
- [Django]-How can I resolve 'django_content_type already exists'?
- [Django]-Actions triggered by field change in Django
- [Django]-Open file in Django app