Connect your Django project to a broker¶
We will install momotor-django, point it at a Momotor broker, and ask
the broker for its server information. You need a Django project, a running
broker, and an API key and secret for that broker.
Install the package in your project’s virtual environment:
python -m pip install momotor-django
Add the app to
INSTALLED_APPSin your settings module:INSTALLED_APPS = [ # ... your other apps ... 'momotor.django', ]
The app label is
momotor. The app provides the database table that the default token store uses.Add the
MOMOTOR_BROKERsetting. At minimum, set your API key and secret:MOMOTOR_BROKER = { 'API_KEY': 'your-api-key', 'API_SECRET': 'your-api-secret', }
With only these two values, the package connects to a broker on
localhostover SSL. SetHOSTandPORTwhen your broker runs elsewhere. See Settings for every value in this dictionary.Create the token table:
python manage.py migrate momotor
Ask the broker for its server information. Broker calls are asynchronous, so create the connection inside async code:
from momotor.django.connection import BrokerConnection async def broker_version() -> str: connection = BrokerConnection() info = await connection.server_info() return info.version
On the first call, the connection logs in, saves the session token through the configured token store, and reuses that token on later calls. The connection logs a line like this on the
momotor.djangologger:connected to Momotor broker version 5.6.0 at localhost:50052, remote protocol version 6.1.0, using OpenSSL 3.0.0
The call returns the broker version string, for example
'5.6.0'.
Next steps¶
Broker connection explains how to call broker methods, follow job status, and choose between a shared and a private channel.
Settings lists every
MOMOTOR_BROKERvalue.Token stores explains where the session token is saved and how to pick a different store.