【问题标题】:Django 1.6: How to ignore a fixture in python manage.py loaddata?Django 1.6:如何忽略 python manage.py loaddata 中的夹具?
【发布时间】:2016-05-24 04:48:36
【问题描述】:

我需要一个答案,现在我正在执行这个命令:

python manage.py loaddata app/myapp/fixtures/* --settings=setting.develop

这工作正常,但现在我想做同样的命令,但忽略或跳过 app/myapp/fixtures/ 中的一个简单文件,所以我不想为里面的每个夹具写一个加载数据,我只想做一个命令并使用 --exclude 或 --ignore 之类的东西或某种方式在一行中执行此操作,并保持 /* 再次检查里面的所有文件。

提前致谢!

【问题讨论】:

  • 我不相信loaddata 有任何此类排除或忽略选项。也许您可以将所有想要包含的灯具组合到一个更大的灯具中,然后按名称导入?
  • 根据您的具体用例,您可能倾向于使用 shell 脚本或您自己的使用 call_command 的管理命令来自动化您想要的装置的 loaddata

标签: python django django-1.6 django-fixtures loaddata


【解决方案1】:

your own management command in Django 很简单;并且从 Django 的 loaddata 命令继承使它变得微不足道:

exclude_loaddata.py

from optparse import make_option

from django.core.management.commands.loaddata import Command as LoadDataCommand


class Command(LoadDataCommand):
    option_list = LoadDataCommand.option_list + (
        make_option('-e', '--exclude', action='append',
                    help='Exclude given fixture/s from being loaded'),
    )

    def handle(self, *fixture_labels, **options):
        self.exclude = options.get('exclude')
        return super(Command, self).handle(*fixture_labels, **options)

    def find_fixtures(self, *args, **kwargs):
        updated_fixtures = []
        fixture_files = super(Command, self).find_fixtures(*args, **kwargs)
        for fixture_file in fixture_files:
            file, directory, name = fixture_file

            # exclude a matched file path, directory or name (filename without extension)
            if file in self.exclude or directory in self.exclude or name in self.exclude:
                if self.verbosity >= 1:
                    self.stdout.write('Fixture skipped (excluded: %s, matches %s)' %
                                      (self.exclude, [file, directory, name]))
            else:
                updated_fixtures.append(fixture_file)
        return updated_fixtures

用法

$ python manage.py excluding_loaddata app/fixtures/* -e not_this_fixture

【讨论】:

    猜你喜欢
    • 2014-07-10
    • 2019-05-11
    • 2010-11-30
    • 2014-08-14
    • 2021-10-13
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多