[Django]-Integrating Dagster with Django

2👍

I would also use the Django custom management command as Timothé said to execute a function or access ORM in the Django’s context.

However, if your use case is required to access the Django ORM directly, you’ll need to do a few steps:

  1. Add the Django project to sys.path, so that Python can find packages.
  2. Set up the Django environment.
import os
import sys

import django
from dagster import execute_pipeline, pipeline, solid


# Add the project to sys.path, so that Python can find packages
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), 'demo')
sys.path.append(PROJECT_ROOT)

# Set up the Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo.settings')
django.setup()


from customers.models import Customer


@solid
def hello_django_orm(context):
    number_of_customers = Customer.objects.count()
    message = f'Found {number_of_customers} customers!'
    context.log.info(message)
    return number_of_customers


@pipeline
def hello_django_orm_pipeline():
    hello_django_orm()


if __name__ == '__main__':
    result = execute_pipeline(hello_django_orm_pipeline)
    assert result.success

See the result below.

enter image description here

Kindly see the full example here.

👤zkan

2👍

As far as I know, there is no high-level integration, but I found some workarounds to launch a pipeline from a django view:

  • synchronously run a pipeline by calling package function execute_pipeline(<your_pipeline>) from python code, for example inside a View (even if it is a non-sense in terms of synchronous/asynchronous paradigm)

  • create a custom management command and call it from inside python code with the django.core.management.call_command() hook

Leave a comment