File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change
1
+ import os
2
+
3
+ from idom .core .layout import Layout , LayoutUpdate
4
+
5
+
6
+ class DjangoLayout (Layout ):
7
+ """Fixes Django ORM usage within components.
8
+ These issues are caused by async/sync mixed context limitations in the ORM.
9
+ Without this, `SynchronousOnlyOperation` exceptions occur when using the ORM in IDOM components.
10
+ This may be fixed in a future version, such as Django 5.0."""
11
+
12
+ def _create_layout_update (self , old_state ) -> LayoutUpdate :
13
+ """Create a layout update, but set ALLOW ASYNC UNSAFE flags prior.
14
+ This allows the Django ORM to be used within components."""
15
+ os .environ ["DJANGO_ALLOW_ASYNC_UNSAFE" ] = "true"
16
+ layout_update = super ()._create_layout_update (old_state )
17
+ os .environ .pop ("DJANGO_ALLOW_ASYNC_UNSAFE" )
18
+ return layout_update
Original file line number Diff line number Diff line change 8
8
from channels .auth import login
9
9
from channels .db import database_sync_to_async as convert_to_async
10
10
from channels .generic .websocket import AsyncJsonWebsocketConsumer
11
- from idom .core .layout import Layout , LayoutEvent
11
+ from idom .core .layout import LayoutEvent
12
12
from idom .core .serve import serve_json_patch
13
13
14
14
from django_idom .config import IDOM_REGISTERED_COMPONENTS
15
15
from django_idom .hooks import IdomWebsocket , WebsocketContext
16
+ from django_idom .layout import DjangoLayout
16
17
17
18
18
19
_logger = logging .getLogger (__name__ )
@@ -73,7 +74,7 @@ async def _run_dispatch_loop(self):
73
74
self ._idom_recv_queue = recv_queue = asyncio .Queue ()
74
75
try :
75
76
await serve_json_patch (
76
- Layout (WebsocketContext (component_instance , value = socket )),
77
+ DjangoLayout (WebsocketContext (component_instance , value = socket )),
77
78
self .send_json ,
78
79
recv_queue .get ,
79
80
)
You can’t perform that action at this time.
0 commit comments