@@ -22,18 +22,23 @@ Here's an example of what that ``asgi.py`` might look like:
22
22
23
23
import os
24
24
25
- from channels.auth import AuthMiddlewareStack
26
- from channels.routing import ProtocolTypeRouter, URLRouter
27
25
from django.conf.urls import url
28
26
from django.core.asgi import get_asgi_application
29
27
30
- from chat.consumers import AdminChatConsumer, PublicChatConsumer
31
-
28
+ # Fetch Django ASGI application early to ensure AppRegistry is populated
29
+ # before importing consumers and AuthMiddlewareStack that may import ORM
30
+ # models.
32
31
os.environ.setdefault(" DJANGO_SETTINGS_MODULE" , " mysite.settings" )
32
+ django_asgi_app = get_asgi_application()
33
+
34
+ from channels.auth import AuthMiddlewareStack
35
+ from channels.routing import ProtocolTypeRouter, URLRouter
36
+
37
+ from chat.consumers import AdminChatConsumer, PublicChatConsumer
33
38
34
39
application = ProtocolTypeRouter({
35
40
# Django's ASGI application to handle traditional HTTP requests
36
- " http" : get_asgi_application() ,
41
+ " http" : django_asgi_app ,
37
42
38
43
# WebSocket chat handler
39
44
" websocket" : AuthMiddlewareStack(
@@ -44,7 +49,6 @@ Here's an example of what that ``asgi.py`` might look like:
44
49
),
45
50
})
46
51
47
-
48
52
Setting up a channel backend
49
53
----------------------------
50
54
0 commit comments