29

无论如何要加载一个夹具并让它加载多个夹具?

理想情况下,我想输入:

python manage.py loaddata all_fixtures

并让它加载所有数据,而不必键入所有内容。这可能吗?

4

12 回答 12

43

使用$ python manage.py loaddata myfixtures/*.json会起作用,因为 Bash 会将通配符替换为匹配的文件名列表。

于 2010-02-09T12:50:32.357 回答
25

我在项目目录中有多个应用程序,并且每个应用程序都有其“fixtures”目录。所以使用一些 bash 我可以做到:

python3 manage.py loaddata */fixtures/*.json

这扩展了我项目中每个应用程序的 fixtures 目录中的所有 json 文件。您只需执行以下操作即可对其进行测试:

ls */fixtures/*.json

于 2015-05-14T13:39:08.657 回答
13

为什么不创建一个包含所有装置的 Makefile?例如:

load_all_fixtures: 
    ./manage.py loaddata path/to/fixtures/foo.json
    ./manage.py loaddata path/to/fixtures/bar.json
    ./manage.py loaddata path/to/fixtures/baz.json

然后在 shell 提示符下,运行

make load_all_fixtures

(这种方法也适用于仅对某些应用程序执行单元测试而忽略其他应用程序,如果需要)

于 2010-02-09T14:38:54.097 回答
13

该线程出现在谷歌搜索“从所有灯具中加载数据”的第一个结果中,并且没有提到 IMO 是正确的解决方案,即允许您加载任何您想要的灯具而无需任何通配符技巧的解决方案,也对 settings.py 文件的一次修改(我也曾经这样做过)

只需使您的应用程序的固定装置目录平坦(而不是通常的 Django 方案,例如 app_name/templates/app_name/mytemplate.html),即 app_name/fixtures/myfixture.[json, yaml, xml]

这是django 文档所说的:

例如:

django-admin loaddata foo/bar/mydata.json

将为每个已安装的应用程序搜索 /fixtures/foo/bar/mydata.json,为 FIXTURE_DIRS 中的每个目录搜索 /foo/bar/mydata.json,以及文字路径 foo/bar/mydata.json。

这意味着如果你在所有应用程序目录中有一个 fixtures/myfixture.json,你只需要运行

./manage.py loaddata myfixture

加载项目中位于那里的所有固定装置......就是这样!您甚至可以通过使用 --app 或 --exclude 参数来限制您加载固定装置的应用程序。

我会提到我在进行一些开发时只使用我的固定装置来填充我的数据库,所以我不介意在我的“固定装置”目录中有一个平面结构......但即使你使用固定装置进行测试,它似乎也有扁平结构是 Django 风格的方式,正如 该答案所暗示的那样,您只需编写如下内容即可引用特定应用程序中的固定装置:

class MyTestCase(TestCase):
    fixtures = ['app_name/fixtures/myfixture.json']
于 2017-10-16T09:06:09.233 回答
5

我的命令是这样的,很简单。(django 1.6)

python manage.py loaddata a.json b.json c.json
于 2014-06-20T09:48:55.623 回答
4

如果您想在 linux 和 windows 上进行这项工作,您只需使用它来加载所有 json-Fixtures:

import os
files = os.listdir('path/to/my/fixtures')

def loaddata(file):
    if os.path.splitext(file)[1] == '.json' and file != 'initial_data.json':
        print file
        os.system("python manage.py loaddata %s" % file)

map(loaddata, files)
于 2012-02-25T10:36:18.177 回答
2

适用于我的 Django-admin 版本 3.1.4

python manage.py loaddata json_file_1 json_file_2

我的文件夹结构是这样的 -

app_name_1
├──fixtures
├────json_file_1.json
├────json_file_2.json
app_name_2
├──fixtures
├────json_file_3.json
于 2020-12-25T11:21:51.477 回答
1

Manage.py loaddata会在某些地方自动显示,因此如果您在每个应用程序中将您的固定装置命名为相同,或者将所有固定装置放在同一个文件夹中,则可以轻松加载它们。如果您有许多不同的灯具,并且需要更复杂的命名模式,您可以使用 find 和 -exec 轻松加载所有灯具

寻找 。-name "*.json" -exec manage.py loaddata {} \;

正如我在此声明的[question][2],我在 fabfile 中也有这个。编辑:如果 manage.py 不在您的 VE 路径中,请使用 python manage.py。

于 2011-05-31T11:07:22.373 回答
1

如果您的灯具位于同一个文件夹中,您可以简单地lsxargs: ls myfolder | xargs django-admin loaddata

这种结构的例子:

$ tree fixtures/
root_dir/fixtures/
├── 1_users.json
├── 2_articles.json
└── 3_addresses.json
$ ls -d fixtures/* | xargs django-admin loaddata 

会做同样的事情:

$ django-admin loaddata 1_users.json
$ django-admin loaddata 2_articles.json
$ django-admin loaddata 3_addresses.json
于 2020-03-23T14:17:09.867 回答
0

This works well for me; it finds all files located in fixtures directories within the the src directory:

python manage.py loaddata \
    $(ls -1 src/**/fixtures/* | tr '\n' '\0' | xargs -0 -n 1 basename | tr '\n' ' ')
于 2019-01-25T05:51:11.240 回答
0

经过一番搜索,我最终编写了这个脚本。它在所有名为“fixtures”的目录中搜索 .json 文件并运行“python manage.py loaddata {fixture_name}.json”。有时排序对于外键约束很重要,因此如果约束无法解决,它会在队列中留下一个固定装置。

(注意:它需要我编写的 pip 包 simple_terminal。我将它设置为由'python manage.py runscript'运行,这需要 django-extensions。)

# load_fixture.py
#
# A script that searches for all .json files in fixtures directories
# and loads them into the working database. This is meant to be run after
# dropping and recreating a database then running migrations.
#
# Usage: python manage.py runscript load_fixtures

from simple_terminal import Terminal

from django.core.management import call_command
from django.db.utils import IntegrityError


def load_fixture(fixture_location):
    # runs command: python manage.py loaddata <fixture_location>
    call_command('loaddata', fixture_location)


def run():
    with Terminal() as t:
        # get all .json files in a fixtures directory
        fixture_locations = t.command(
            'find . -name *.json | grep fixtures | grep -v env')

    while fixture_locations:
        # check that every iteration imports are occuring
        errors = []
        length_before = len(fixture_locations)

        for fl in fixture_locations:
            try:
                # try to load fixture and if loaded remove it from the array
                load_fixture(fl)
                print("LOADED: {}".format(fl))
                fixture_locations.remove(fl)
            except IntegrityError as e:
                errors.append(str(e))

        # if import did not occur this iteration raise exception due to
        # missing foreign key reference
        length_after = len(fixture_locations)
        if length_before == length_after:
            raise IntegrityError(' '.join(errors))
于 2017-11-16T19:29:51.913 回答
0
python manage.py loaddata ./*/fixtures/*.json

该命令将fixture在所有目录中查找该文件夹,然后它将获取所有带有json扩展名的文件并安装灯具。

这样,您不必只有一个文件夹用于存放灯具,而是可以在应用级别和多个应用中拥有灯具。

它将与这样的理想文件夹结构一起使用 -

app_name_1
├──fixtures
├────json_file_1.json
├────json_file_2.json
app_name_2
├──fixtures
├────json_file_3.json
于 2022-01-26T06:36:35.437 回答