Alex Mercer
Alex Mercer

Reputation: 495

Send data to users base on database event in Django channels

I have an application in Django that some users can add and update data. these users add or update data with Django standard forms and views.

I want to implement an other app that send new data to all users when a user update or create data in database. i read about Django-channels that can handle web socket, but i can't find something about server or database events in Django-channels.

So how can i send data to users when a database event occur?

Upvotes: 1

Views: 599

Answers (1)

Ozgur Akcali
Ozgur Akcali

Reputation: 5492

You can use Signals to detect database events in Django. Take a look at the signals explained here: https://docs.djangoproject.com/en/2.1/topics/signals/

Basically, you'll be doing something along the lines of:

@receiver(post_save, sender=ModelClass)
def my_model_save(sender, instance, **kwargs):
     # this code will be executed after an instance of ModelClass is saved.
     Group(rate_key).send({
            "text": "my message"
     })

Upvotes: 1

Related Questions