4. Django & Lightbus

If you get stuck...

It is really useful to hear from people who have encountered a problem or got stuck. Hearing from you means we can improve our documentation and error messages.

If you get stuck drop then please drop an email to adam@adamcharnock.com, visit the Lightbus discord server, or call me (Adam) on +442032896620.

The more information you can include the better (problem description, screenshots, and code are all useful)

Lightbus is designed to work smoothly with Django. A few of additions are needed to your bus.py file in order to setup django correctly. You should perform these at the very start of your @bus.client.on_start() function within your bus.py:

  1. Set your DJANGO_SETTINGS_MODULE environment variable1
  2. Call django.setup()
  3. Decorate RPCs and handlers which use the Django ORM with @uses_django_db. This will ensure database errors are cleaned up correctly.

Simple example

The following example demonstrates:

  2. Calling django.setup()
# bus.py
import lightbus
import django
import os
# Do NOT import your django models here

bus = lightbus.create()

# ...Define and register APIs here...

def on_start(**kwargs):
    # Customise this value (if necessary) to point to your settings module. 
    # This should be the same as the corresponding line in your manage.py file.
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

    # DJANGO STEP 2: Call django.setup()
    # We must do this before we import any of our django models

    # You can now safely import your django models here if needed,
    # or within any event handlers we setup (see next example)
    from my_app.models import MyModel

Practical example

Here we build upon the simple example (above). This example includes an API and an event handler, and is loosely based upon our work in the quick start tutorial. Here we demonstrate:

  2. Calling django.setup()
  3. Applying @uses_django_db to an RPC and event handler
import lightbus
from lightbus.utilities.django import uses_django_db
import django
import os

bus = lightbus.create()

class AuthApi(lightbus.Api):
    user_registered = lightbus.Event(parameters=('username', 'email'))

    class Meta:
        name = 'auth'

    # Decorating our RPC with @uses_django_db  
    # ensures database connections are cleaned up
    def check_password(self, username, password):
        # Import django models here, once django.setup() has been called
        from django.contrib.auth.models import User
            user = User.objects.get(username=username)
            return user.check_password(password)
        except User.DoesNotExist:
            return False


# Decorating our event handler with @uses_django_db  
# ensures database connections are cleaned up
def handle_new_user(event, username, email):
    # For example, we may want to create an audit log entry
    from my_app.models import AuditLogEntry
    AuditLogEntry.objects.create(message=f"User {username} created")

def on_start(**kwargs):
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_company.a_project.settings")

    # Setup a listener for the 'auth.user_registered' event, to be 
    # handled by handle_new_user() (above)

Further development

Simplification to the Django setup process is tracked in GitHub.

  1. You will see that your Django project' manage.py file also performs this same step