【问题标题】:Integrating Dagster with Django将 Dagster 与 Django 集成
【发布时间】:2020-03-07 22:20:30
【问题描述】:

您好,我正在尝试将 Dagster 集成到正在进行的 Django 项目中。我在为 Dagster 提供 Django 上下文(模型、应用程序等)方面有些挣扎。截至目前,我只是在使用 Dagster 的应用的 init.py 中检查 sys.argv[0] 中是否存在 dagit

<!-- language: python -->
## project/app/__init__.py
import sys

import django

if 'dagit-cli' in sys.argv[0]:
    print('dagit')
    django.setup()

谁能帮我设置一下?

【问题讨论】:

  • 您需要给我们一些背景信息。什么是 Dagster?
  • 添加了超链接

标签: python django dagster


【解决方案1】:

我还将使用 Django 自定义管理命令,正如 Timothé 所说,在 Django 的上下文中执行函数或访问 ORM。

但是,如果您的用例需要直接访问 Django ORM,则需要执行几个步骤:

  1. 将Django项目添加到sys.path,以便Python可以找到包。
  2. 设置 Django 环境。
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

查看下面的结果。

请查看完整示例here

【讨论】:

    【解决方案2】:

    据我所知,没有高级别的集成,但我找到了一些从 django 视图启动管道的变通方法:

      1234563 /li>
    • 创建自定义管理命令并使用django.core.management.call_command() 挂钩从python 代码内部调用它

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 2016-03-09
      • 2021-09-15
      • 2013-05-21
      • 2014-10-24
      • 2018-04-12
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多