# Django Async View with ORM and thread\_sensitive=False

**URL:** <https://forum.djangoproject.com/t/django-async-view-with-orm-and-thread-sensitive-false/7112>\
**Category:** Using Django\
**Created:** [March 18, 2021, 5:14pm UTC](https://forum.djangoproject.com/t/django-async-view-with-orm-and-thread-sensitive-false/7112 "2021-03-18T17:14:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![renatohr](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renatohr/32/3893_2.png) [@renatohr](https://forum.djangoproject.com/u/renatohr)\
**Post date:** [March 18, 2021, 5:14pm UTC](https://forum.djangoproject.com/t/django-async-view-with-orm-and-thread-sensitive-false/7112/1 "2021-03-18T17:14:05Z")

</div>

Is it fundamentally wrong to use an async view with thread\_sensitive=False performing an Django ORM call?

For instance:

```auto
def call_to_a_3rd_party_API():
	something = requests.post(...)
	return something

@sync_to_async(thread_sensitive=False)
def do_something_to_a_product(request, id):
    product = Product.objects.get(pk=id)
    result = call_to_a_3rd_party_API()
    product.some_value = result
    product.update() ​
    ​return HttpResponse(status=200)

```

The goal here is that I am migrating some of my Django 2.2 views due to blocking IO behaviour (from calling 3rd party APIs) to Django 3.1 async views. In my tests, the only way I could turn the 3rd party API call in non-blocking is using thread\_sensitive=False.

Reading the docs ([Asynchronous support | Django documentation | Django](https://docs.djangoproject.com/en/3.1/topics/async/)) it feels like we should never use thread\_sensitive=False with Django ORM. So the alternative I guessed would be:

```auto
@sync_to_async(thread_sensitive=False)
def call_to_a_3rd_party_API():
	something = requests.post(...)
	return something

async def do_something_to_a_product(request, id):
    product = await sync_to_async(Product.objects.get, thread_sensitive=True)(Product, pk=id)
    result = await call_to_a_3rd_party_API()
    product.some_value = result
    await sync_to_async(product.save, thread_sensitive=True)
   ​return HttpResponse(status=200)

```

It feels like this way I can turn the call\_to\_a\_3rd\_party\_API() into a non-blocking function, while keeping all the Django ORM in the main thread. But the code feels a lot more complex.

Could someone help me with this?

Extra question: I have two projects, one of them using wsgi and the other asgi. Does it make a difference regarding the issue above?

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [March 21, 2021, 5:33pm UTC](https://forum.djangoproject.com/t/django-async-view-with-orm-and-thread-sensitive-false/7112/2 "2021-03-21T17:33:50Z")

</div>

I think I’d try to write this like the following:

```auto
@sync_to_async
async def update_product(id, result):
    product = Product.objects.get(pk=id)
    product.some_value = result
    product.save()

async def do_something_to_a_product(request, id):
    result = await call_to_a_3rd_party_API()
    await update_product(id, result)
   ​return HttpResponse(status=200)

```

The case in which your approach would be required is if you want to validate that the instance exists before hitting the 3rd party API.

---

<div class="post-metadata">

**Author:** ![renatohr](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renatohr/32/3893_2.png) [@renatohr](https://forum.djangoproject.com/u/renatohr)\
**Post date:** [March 21, 2021, 6:36pm UTC](https://forum.djangoproject.com/t/django-async-view-with-orm-and-thread-sensitive-false/7112/3 "2021-03-21T18:36:17Z")

</div>

Hey Tim, indeed that is a good suggestion of code. Regarding the thread\_sensitiveness of Django ORM, should I always keep it True?

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [March 21, 2021, 7:16pm UTC](https://forum.djangoproject.com/t/django-async-view-with-orm-and-thread-sensitive-false/7112/4 "2021-03-21T19:16:41Z")

</div>

I haven’t dug into this myself, but I would utilize the defaults until I found a reason why they won’t work. Also, I’m not sure if you’re using Django Channels, but there’s [some good information there](https://channels.readthedocs.io/en/stable/topics/databases.html#database-access). If you’re not, you should probably consider making your own version of `database_sync_to_async` from [channels/db.py at main · django/channels · GitHub](https://github.com/django/channels/blob/main/channels/db.py)

---

<div class="post-metadata">

**Author:** ![renatohr](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renatohr/32/3893_2.png) [@renatohr](https://forum.djangoproject.com/u/renatohr)\
**Post date:** [March 21, 2021, 8:55pm UTC](https://forum.djangoproject.com/t/django-async-view-with-orm-and-thread-sensitive-false/7112/5 "2021-03-21T20:55:05Z")

</div>

Thanks a lot! I will look into It. Yes, I am using channels with gunicorn+uvicorn. Thanks Tim!
