Python Django调用带有文件名通配符的_命令()
如何在Python Django调用带有文件名通配符的_命令(),python,django,Python,Django,如何在call_command()中使用文件名通配符?我正在尝试创建一个管理命令,其作用与python manage.py loaddata*/fixtures/*.json 下面的代码抛出CommandError:未找到名为“*”的装置。 from django.core.management import call_command from django.core.management.base import BaseCommand class Command(BaseCommand):
call_command()
中使用文件名通配符?我正在尝试创建一个管理命令,其作用与python manage.py loaddata*/fixtures/*.json
下面的代码抛出CommandError:未找到名为“*”的装置。
from django.core.management import call_command
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Load all fixtures in app directories'
def handle(self, *args, **kwargs):
call_command('loaddata', '*/fixtures/*.json')
self.stdout.write('Fixtures loaded\n')
python manage.py loaddata*/fixtures/*.json
命令中的glob输入有效,因为glob由bash扩展;如果尝试转义glob,例如,python manage.py loaddata'*/fixtures/*.json'
,该命令将失败并显示相同的错误消息
相反,在Python端展开globs,例如:
import pathlib
class Command(BaseCommand):
help = 'Load all fixtures in app directories'
def handle(self, *args, **kwargs):
cmd_args = list(pathlib.Path().glob('*/fixtures/*.json'))
call_command('loaddata', *cmd_args)