Django channels: How to reference ForeignKey and their fields in async context?

39 Views Asked by At

I am right now converting a couple of Websocket-Handlers from WebsocketConsumer to AsyncWebsocketConsumer. There is one line that causes problems. The Sync version was:

self.mydatacache['workernr'] = school.objects.get(id=myschool).tf_worker.id

My first try for an async version was this:

self.mydatacache['workernr'] = await school.objects.aget(id=myschool).tf_worker.id

Second try:

school_object = await school.objects.aget(id=myschool)
tf_worker_object = await school_object.tf_worker   
self.mydatacache['workernr'] = tf_worker_object.id

I tried many variations of this, but I always got the same error:

You cannot call this from an async context

I must be on the completely wrong track here. Any suggestions?

PS: I am aware of database_sync_to_async. If possible I would like to avoid that...

0

There are 0 best solutions below