Использование channels_layer в модели django [закрыто].

hello Я хочу связать мой класс Notification с моим websocket так, чтобы я зарегистрировал новый экземпляр объекта уведомления, чтобы я мог получить сообщение через мой websocket К сожалению, мой код не работает, как я ожидал Класс уведомления здесь мой :

class Notification(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
notification = models.TextField(max_length=300)
is_seen = models.BooleanField(default=False)

def __str__(self):
    string = f'{self.user} ({self.notification}):  {"False" if self.is_seen is False else"True"}'
    return string

def save(self, force_insert=False, force_update=False, using=None,
         update_fields=None):
    channel_layer = get_channel_layer()
    notification_count = Notification.objects.filter(is_seen=False).count()
    data = {'count': notification_count,
            'courrent_notification': self.notification}
    async_to_sync(channel_layer.group_send)(
        'notification_groupe', {
            'type': 'disconnect',
            'value': json.dumps(data)})
    super(Notification, self).save()

моя модель потребителя:

class Notification_consumer(WebsocketConsumer):

def connect(self):
    self.room_group_name = 'notification_groupe'
    self.room_name = 'notification_consumer'
    async_to_sync(self.channel_layer.group_add)(
        self.room_group_name,
        self.room_name
    )
    self.accept()
    self.send(json.dumps({"status": "connected to notification consumer!"}))

def receive(self, text_data=None, bytes_data=None):
    print(text_data)
    self.send(json.dumps({"status": "notification consumer a votre service"}))

def send_notification(self, event):
    print('send_notification')
    print(event)
    print('send_notification')

def disconnect(self, code):
    self.send(json.dumps({"status": "déconnexion effectuée!"}))
    print('déconnecxion')
Вернуться на верх