我正在学习如何在我的网站中实现Django频道,但我对文档(http://channels.readthedocs.io/en/latest/binding.html)有所了解。互联网上的例子并不多。有人能为我提供一个有效的评论数据绑定的工作源代码吗?
答案 0 :(得分:0)
对于django-channels 1 - https://bitbucket.org/voron-raven/chat/src/aec8536dba2cc5f0faa42305dcd7a49d330a8b54/core/models.py?at=master&fileviewer=file-view-default#models.py-242 它是一个简单的聊天网站聊天机器人 - http://chat.mkeda.me
数据绑定示例:
class MessageBinding(WebsocketBinding):
model = Message
stream = 'messages'
fields = ['__all__']
@classmethod
def group_names(cls, instance):
"""
Returns the iterable of group names to send the object to based on the
instance and action performed on it.
"""
return ['thread-{}'.format(instance.thread.pk)]
def has_permission(self, user, action, pk):
"""
Return True if the user can do action to the pk, False if not.
User may be AnonymousUser if no auth hooked up/they're not logged in.
Action is one of "create", "delete", "update".
"""
if action == 'create':
return True
return user.is_superuser
在django-channels中删除了2个绑定 - http://channels.readthedocs.io/en/latest/one-to-two.html?highlight=binding#removed-components 您可以使用信号向您的群组发送更新。