如何在call_command()
中使用文件名通配符?我正在尝试创建一个与python manage.py loaddata */fixtures/*.json
相同的管理命令
下面的代码抛出CommandError: No fixture named '*' found.
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')
发布于 2019-06-11 23:17:07
python manage.py loaddata */fixtures/*.json
命令中的glob输入有效,因为bash扩展了glob;如果您尝试转义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)
https://stackoverflow.com/questions/56545965
复制相似问题