从Django Management命令

时间:2018-04-16 14:20:45

标签: python django django-management-command

让我知道这个问题是否足够明显,而且这个问题也有重复,我事先未能进行搜索。

我正在尝试从Django管理命令运行pip install requirements/dev.txt。我有一个逻辑,如果缓存不变,不要尝试运行脚本pip ...。我想弄清楚如何从DMC运行这个命令?

用法:

python manage.py install_prepreqs
Cache is unchanged, skipping.... 

install_prepreqs.py

# I want to run `pip install requirements/dev.txt` with some additional logic. 

1 个答案:

答案 0 :(得分:0)

从Django内部运行pip可能不是一个好主意,但是如果你愿意,那就自己敲门:djangos custom-management-commandspythons subprocess module

另请参阅this post有关子流程模块的信息。

from django.core.management.base import BaseCommand, CommandError
import subprocess

class Command(BaseCommand):
    def handle(self, *args, **options):
        # check your precondition logic here
        # ... your code goes here ...

        if my_condition == True:
            subprocess.run(["ls", "-l"])

        # ... some more code goes here if you wish ...
相关问题