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

53👍

It works for me

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

BTW, I start my notebook using the command

./manage.py shell_plus --notebook

10👍

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.

5👍

For now I plan on just using a forked version of django with a new setting to skip the async_unsafe check. Once the ORM gets async support I’ll probably have to rewrite my project to support it and drop the flag.

EDIT: there’s now a PR to add an env variable (DJANGO_ALLOW_ASYNC_UNSAFE) to disable the check (https://github.com/django/django/pull/12172)

1👍

I added

os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" code in the setting.py of your project all the way down to the file and then

command python3 manage.py shell_plus --notebook

if you are using python3 or use python

That’s it. worked for me.

Leave a comment