Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listen on multiple events for the same channel #86

Open
RaitoBezarius opened this issue Nov 20, 2024 · 1 comment
Open

Listen on multiple events for the same channel #86

RaitoBezarius opened this issue Nov 20, 2024 · 1 comment

Comments

@RaitoBezarius
Copy link

Using the same channel, it seems like we cannot listen on multiple events, is that on purpose?

I'd have expected to write "one channel" per model, but it feels like we need to write as many channels as we have events need.

Would that make sense to document in the FAQ?

@PaulGilmartin
Copy link
Owner

@RaitoBezarius Can you clarify what you mean with an example?

Channels and listeners behave much like Django signals. A model can be associated to a channel and then a channel can be associated to as many listener functions as we want. From the unit tests for example:

@atomic
@pgpubsub.post_insert_listener(AuthorTriggerChannel)
def create_first_post_for_author(
        old: Author, new: Author, context: Optional[Dict[str, Any]] = None
):
    print(f'Creating first post for {new.name}')
    content = 'Welcome! This is your first post'
    if context and 'content' in context:
        content = context.get('content')
    Post.objects.create(
        author_id=new.pk,
        content=content,
        date=datetime.date.today(),
    )


@pgpubsub.post_insert_listener(AuthorTriggerChannel)
def another_author_trigger(
        old: Author, new: Author, context: Optional[Dict[str, Any]] = None
):
    print(f'Another author trigger')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants