From 5eb3bf848c77c8dc39c201ca388e3e0e034cb843 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Mon, 27 Jun 2016 16:46:47 -0700 Subject: [PATCH] Provide keyword args as self.kwargs in CBC (ref. #224) --- channels/generic/base.py | 1 + docs/generics.rst | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/channels/generic/base.py b/channels/generic/base.py index 73d97fc..b54d703 100644 --- a/channels/generic/base.py +++ b/channels/generic/base.py @@ -26,6 +26,7 @@ class BaseConsumer(object): the uninstantiated class, so calling it creates it) """ self.message = message + self.kwargs = kwargs self.dispatch(message, **kwargs) @classmethod diff --git a/docs/generics.rst b/docs/generics.rst index 97c013a..7edda19 100644 --- a/docs/generics.rst +++ b/docs/generics.rst @@ -32,7 +32,8 @@ Here's a routing example:: Class-based consumers are instantiated once for each message they consume, so it's safe to store things on ``self`` (in fact, ``self.message`` is the -current message by default). +current message by default, and ``self.kwargs`` are the keyword arguments +passed in from the routing). Base ---- @@ -62,6 +63,13 @@ If you want to perfom more complicated routing, you'll need to override the remember, though, your channel names cannot change during runtime and must always be the same for as long as your process runs. +``BaseConsumer`` and all other generic consumers than inherit from it provide +two instance variables on the class: + +* ``self.message``, the :ref:`Message object ` representing the + message the consumer was called for. +* ``self.kwargs``, keyword arguments from the :doc:`routing` + WebSockets ----------