【发布时间】:2017-09-05 10:34:48
【问题描述】:
我正在学习如何在我的网站中实现 Django 频道,但我无法理解文档 (http://channels.readthedocs.io/en/latest/binding.html)。互联网上的例子并不多。有人可以为我提供一个有效的数据绑定源代码并附上不错的评论吗?
【问题讨论】:
标签: django data-binding django-channels
我正在学习如何在我的网站中实现 Django 频道,但我无法理解文档 (http://channels.readthedocs.io/en/latest/binding.html)。互联网上的例子并不多。有人可以为我提供一个有效的数据绑定源代码并附上不错的评论吗?
【问题讨论】:
标签: django data-binding django-channels
对于 django 频道 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 您可以使用信号向您的群组发送更新。
【讨论】: