Data Binding Example Channels 2

44 views
Skip to first unread message

weezar...@gmail.com

unread,
Aug 22, 2018, 2:47:07 PM8/22/18
to Django users
Hello,

In another question it was pointed out that data binding has been removed form the channels 2.x core. Andrew said "You can write most of the data binding functionality yourself in Channels 2 by adding group send methods to model save methods" - can anyone provide a basic example of how to properly import and use the consumer to send communication this way (I presume that the model save method would be overloaded in the models.py and the consumer would be imported and called there)?

Thanks in advance

Mikhailo Keda

unread,
Aug 22, 2018, 3:12:58 PM8/22/18
to Django users
There is an example of simple chat site (direct link to save method where data binding is implemented):
https://bitbucket.org/voron-raven/chat/src/f78b6531652f866109dbfa2f8aeffac6c0f1bb32/core/models.py#lines-157

Mikhailo Keda

unread,
Aug 22, 2018, 3:16:42 PM8/22/18
to Django users
link to the chat site - https://chat.mkeda.me/

середа, 22 серпня 2018 р. 22:12:58 UTC+3 користувач Mikhailo Keda написав:

weezar...@gmail.com

unread,
Aug 22, 2018, 3:30:44 PM8/22/18
to Django users
Thank you very much! This will be extremely helpful in accomplishing my intended usage!

weezar...@gmail.com

unread,
Aug 22, 2018, 3:45:15 PM8/22/18
to Django users
Another thought occurs to me: what are the benefits (if any) of this method vs. hooking into the save / create / delete signals?

weezar...@gmail.com

unread,
Aug 22, 2018, 4:31:38 PM8/22/18
to Django users
Also, if for instance I am updating a user model with information that I want only that specific user to see, how do I send over the websocket to only that user (they would have an authenticated session)?

sorry for the bombardment of questions :P

Mikhailo Keda

unread,
Aug 23, 2018, 1:51:30 AM8/23/18
to Django users
There are no benefits. As for me if you need to implement data binding just for one model - you could do this directly in the model, if you need data binding for few models - better to use signals to keep code clean and readable.

You could create groups with just one user.
Reply all
Reply to author
Forward
0 new messages