title: Saving database changes with django channels
body: <p>I wish to make simple program in django channels - I open Websocket and then listen for users clicking a button or pressing any key down. If such event occurs JS sends message to Channels where it gets access to db where there is a model of a counter, increment it depends it was click or key, and then send it back to group on layers. Unfortunately, error occurs. Why does it call context error if I already used <code>database_sync_to_async</code>?</p>
  <p>My <code>consumers.py</code>:</p>
  <pre class="lang-py prettyprint-override"><code>from channels.generic.websocket import AsyncWebsocketConsumer
  import json
  from channels.db import database_sync_to_async
  from .models import Licznik
      
      class MyConsumer(AsyncWebsocketConsumer):
          async def connect(self):
              self.room_group_name = 'main_room'
              self.counter = await database_sync_to_async(self.get_counter)()
      
              await (self.channel_layer.group_add)(
                  self.room_group_name,
                  self.channel_name
              )
              await self.accept()
      
      
         def get_counter(self):
             return Licznik.objects.all()[0]
      
         async def receive(self, text_data):
              if text_data == &quot;klik&quot;:
                  self.counter.klik +=1
              elif text_data == &quot;klak&quot;:
                  self.counter.key += 1
              await database_sync_to_async(self.counter.save()) #error here
              klik = self.counter.klik
              klak = self.counter.key
              await (self.channel_layer.group_send)(
                  self.room_group_name,
                  {
                      'type': 'chat_message',
                      'klik': klik,
                      'klak': klak
                  }
              )
      
         async def chat_message(self, event):
              message_klik = event['klik']
              message_klak = event['klak']
              await self.send(text_data=json.dumps({
                  'klik': message_klik,
                  'klak': message_klak
              }))
      
         async def disconnect(self, close_code):
              await (self.channel_layer.group_discard)(
                  self.room_group_name,
                  self.channel_name
              )
              await self.close()
  </code></pre>
  <p>Error:</p>
  <pre><code>Exception inside application: You cannot call this from an async context - use a thread or sync_to_async.
  Traceback (most recent call last):
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/channels/sessions.py&quot;, line 183, in __call__
      return await self.inner(receive, self.send)
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/channels/middleware.py&quot;, line 41, in coroutine_call
      await inner_instance(receive, send)
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/channels/consumer.py&quot;, line 58, in __call__
      await await_many_dispatch(
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/channels/utils.py&quot;, line 51, in await_many_dispatch
      await dispatch(result)
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/channels/consumer.py&quot;, line 73, in dispatch
      await handler(message)
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/channels/generic/websocket.py&quot;, line 196, in websocket_receive
      await self.receive(text_data=message[&quot;text&quot;])
    File &quot;/Users/dottore/PycharmProjects/klikator/klikator/klikacz/consumers.py&quot;, line 26, in receive
      await database_sync_to_async(self.counter.save())
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/django/db/models/base.py&quot;, line 753, in save
      self.save_base(using=using, force_insert=force_insert,
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/django/db/models/base.py&quot;, line 790, in save_base
      updated = self._save_table(
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/django/db/models/base.py&quot;, line 872, in _save_table
      updated = self._do_update(base_qs, using, pk_val, values, update_fields,
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/django/db/models/base.py&quot;, line 926, in _do_update
      return filtered._update(values) &gt; 0
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/django/db/models/query.py&quot;, line 803, in _update
      return query.get_compiler(self.db).execute_sql(CURSOR)
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py&quot;, line 1522, in execute_sql
      cursor = super().execute_sql(result_type)
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py&quot;, line 1154, in execute_sql
      cursor = self.connection.cursor()
    File &quot;/Users/dottore/PycharmProjects/klikator/venv/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 24, in inner
      raise SynchronousOnlyOperation(message)
  django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
  </code></pre>