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.

  1. Install the package in your project’s virtual environment:

    python -m pip install momotor-django
    
  2. Add the app to INSTALLED_APPS in 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.

  3. Add the MOMOTOR_BROKER setting. 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 localhost over SSL. Set HOST and PORT when your broker runs elsewhere. See Settings for every value in this dictionary.

  4. Create the token table:

    python manage.py migrate momotor
    
  5. 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.django logger:

    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_BROKER value.

  • Token stores explains where the session token is saved and how to pick a different store.