title: How to use django 3.0 ORM in a Jupyter Notebook without triggering the async context check?
body: <p>Django 3.0 is adding <a href="https://docs.djangoproject.com/en/dev/releases/3.0/#asgi-support" rel="noreferrer">asgi / async support and with it a guard around making synchronous requests in an async context</a>. Concurrently, <a href="https://blog.jupyter.org/ipython-7-0-async-repl-a35ce050f7f7" rel="noreferrer">IPython just added top level async/await support</a>, which seems to be running the whole interpreter session inside of a default event loop. </p>
  
  <p>Unfortunately the combination of these two great addition means that any django ORM operation in a jupyter notebook causes a <code>SynchronousOnlyOperation</code> exception:</p>
  
  <pre><code>SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
  </code></pre>
  
  <p>As the exception message says, it's possible to wrap each ORM call in a <code>sync_to_async()</code> like:</p>
  
  <pre class="lang-py prettyprint-override"><code>images = await sync_to_async(Image.objects.all)()
  </code></pre>
  
  <p>but it's not very convenient, especially for related fields which would usually be implicitly resolved on attribute lookup.</p>
  
  <p>(I tried <code>%autoawait off</code> magic but it didn't work, from a quick glance at <a href="https://ipython.readthedocs.io/en/stable/interactive/autoawait.html#using-autoawait-in-a-notebook-ipykernel" rel="noreferrer">the docs</a> I'm assuming it's because ipykernels always run in an asyncio loop)</p>
  
  <p><strong>So is there a way to either disable the sync in async context check in django or run an ipykernel in a synchronous context?</strong></p>
  
  <hr>
  
  <p>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.</p>