-
How do I filter the list of changes when subscribing to a model? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You should use the Let us consider the example of subscribing to all the comments that To start out we can declare the model observer handler method: @model_observer(Comment)
async def blog_post_comment_changed(self, message, subscribed_request_ids, action, **kwargs):
await self.reply(action=action, data= message, request_id=request_id) With this in place we need to add an action to our consumer so that the user can start subscribing to changes. @action()
async def subscribe_to_comments(self, request_id, **kwargs):
username = self.scope['user'].username
await self. blog_post_comment_changed.subscribe(request_id=request_id, username=username)
await self.reply(action=action, request_id=request_id) Important in the above snippet is the passing of the To do this we add a @blog_post_comment_changed.groups_for_consumer
def blog_post_comment_changed(self, username: str, **kwargs):
yield f"mentioned__{username}"
yield f"reply_to__{username}" This method is called when subscribing to enumerate the list of groups to subscribe to. In this case we subscribe to 2 different groups. The first is a group that will get notified every time a user With the consumer subscribe to these 2 groups we now need to ensure that when a To do this we implement a def blog_post_comment_changed(self, instance: Comment, **kwargs):
for mention in comment.get_mentions():
yield f'mentioned__{mention.username}'
parent = instance.parent
if parent is not None and instance.author != parent.author:
yield f"reply_to__{parent.author.username}" |
Beta Was this translation helpful? Give feedback.
You should use the
@model_observer
.Let us consider the example of subscribing to all the comments that
@mention
the current user.To start out we can declare the model observer handler method:
With this in place we need to add an action to our consumer so that the user can start subscribing to changes.