Skip to content

How do I filter the list of changes when subscribing to a model? #112

Answered by hishnash
hishnash asked this question in FAQ
Discussion options

You must be logged in to vote

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:

@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=u…

Replies: 1 comment

Comment options

hishnash
Feb 23, 2022
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by hishnash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
FAQ
Labels
None yet
1 participant