title: Django chat application
body: <p>I'm trying to make a chat application with Django channels. I've successfully build a one-to-one chat app but I'm not getting the idea for extending it to multiple one-to-one chat. Like, right now there's only two users logged in, but what if multiple users are chatting with one another. I'm stuck on how to prepare the model for that and inserting data in the database. So if anyone can help I'd appreciate that.</p>
  
  <p>models.py</p>
  
  <pre><code>class Chat(models.Model):
      room = models.CharField(max_length = 255, null = True)
      user = models.ForeignKey(User, on_delete = models.SET_NULL, null = True)
      message = models.TextField()
      time = models.DateTimeField(auto_now_add = True, null = True)
  
      def __str__(self):
          return self.user.username
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>def student_messages(request, pk):
      student = Student.objects.get(id = pk)
      chat = Chat.objects.filter(room = request.user)
      if request.is_ajax():
          print(request.POST.get('chat'))
          Chat.objects.create(user = student.user, message = request.POST.get('chat'), room = request.user)
      return render(request, "main/student_messages.html", {"student": student, "chat": chat})
  
  
  def instructor_messages(request, pk):
      instructor = Instructor.objects.get(id = pk)
      chat = Chat.objects.filter(room = request.user)
      notify = InstructorCourseNotification.objects.filter(instructor = instructor).order_by('-id')[:3]
      if request.is_ajax():
          print(request.POST.get('chat'))
          Chat.objects.create(user = instructor.user, message = request.POST.get('chat'), room = request.user)
      return render(request, "main/instructor_messages.html", {"instructor": instructor, "chat": chat, "notify": notify})
  </code></pre>
  
  <p>consumers.py</p>
  
  <pre><code>class chatConsumer(AsyncJsonWebsocketConsumer):
      async def connect(self):
          await self.accept()
          await self.channel_layer.group_add("chat", self.channel_name)
          print(f"{self.channel_name}")
  
      async def disconnect(self, code):
          await self.channel_layer.group_discard("chat", self.channel_name)
          print(f"removed {self.channel_name}")
  
      async def user_chat(self, event):
          await self.send_json(event)
          print(f"{event}")
  </code></pre>
  
  <p>signals.py</p>
  
  <pre><code>@receiver(post_save, sender=Chat)
  def chat_notification(sender, instance, created, **kwargs):
      if created:
          channel_layer = get_channel_layer()
          async_to_sync(channel_layer.group_send)(
              "chat",
              {
              "type": "user.chat",
              "event": "New Chat",
              "user": instance.user.id,
              "message": instance.message,
              "time": json.dumps(instance.time.strftime("%d"), cls = DjangoJSONEncoder)
              })
  
  </code></pre>
  
  <p>The problem is in this current implementation also I cannot successfully save the whole chats. Only the respective users sent messages are saved.</p>