How to use django 3.0 ORM in a Jupyter Notebook without triggering the async context check?

Django 3.0 is adding asgi / async support and with it a guard around making synchronous requests in an async context. Concurrently, IPython just added top level async/await support, which seems to be running the whole interpreter session inside of a default event loop.

Unfortunately the combination of these two great addition means that any django ORM operation in a jupyter notebook causes a SynchronousOnlyOperation exception:

SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

As the exception message says, it's possible to wrap each ORM call in a sync_to_async() like:

images = await sync_to_async(Image.objects.all)()

but it's not very convenient, especially for related fields which would usually be implicitly resolved on attribute lookup.

(I tried %autoawait off magic but it didn't work, from a quick glance at the docs I'm assuming it's because ipykernels always run in an asyncio loop)

So is there a way to either disable the sync in async context check in django or run an ipykernel in a synchronous context?


For context: I wrote a data science package that uses django as a backend server but also exposes a jupyter based interface on top of the ORM that allows you to clean/annotate data, track machine learning experiments and run training jobs all in a jupyter notebook.


It works for me

os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

BTW, I start my notebook using the command

./manage.py shell_plus --notebook

Contrary to other answers I'd suggest just running from the shell as:

env DJANGO_ALLOW_ASYNC_UNSAFE=true ./manage.py shell_plus --notebook

and not modifying any config files or startup scripts.

The advantage of doing it like this is that these checks still seem useful to have enabled almost everywhere else (e.g. when debugging locally via runserver or when running tests). Disabling via files would easily disable these in too many places negating their advantage.

Note that most shells provide easy ways of recalling previously invoked command lines, e.g. in Bash or Zsh Ctrl+R followed by notebook would find the last time you ran something that had "notebook" in. Under the fish shell just type notebook and press the up arrow key to start a reverse search.