一次加载多个夹具

发布于 08-20 21:21 字数 145 浏览 14 评论 0原文

有没有办法加载一个灯具并让它加载多个灯具?

理想情况下,我想输入:

python manage.py loaddata all_fixtures

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

Is there anyway to load one fixture and have it load multiple fixtures?

I'd ideally like to type:

python manage.py loaddata all_fixtures

And have that load all of the data instead of having to type everything. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

浮生未歇2024-08-27 21:21:12

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

Using $ python manage.py loaddata myfixtures/*.json would work as Bash will substitute the wildcard to a list of matching filenames.

这样的小城市2024-08-27 21:21:12

我的项目目录中有多个应用程序,并且每个应用程序都有其“fixtures”目录。因此,使用一些 bash 我可以执行以下操作:

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

这会扩展项目中每个应用程序的fixtures 目录内的所有json 文件。您可以通过简单地执行以下操作来测试它:

ls */fixtures/*.json

I have multiple apps on the project directory and have each app with its 'fixtures' directory. So using some bash I can do:

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

And that expands all of the json files inside of the fixtures directory on each app in my project. You can test it by simply doing:

ls */fixtures/*.json

半世蒼涼2024-08-27 21:21:12

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

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

这是 django doc 说:

例如:

django-admin loaddata foo/bar/mydata.json

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

这意味着,如果您的所有应用程序目录中都有固定装置/myfixture.json,则只需运行即可

./manage.py loaddata myfixture

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

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

class MyTestCase(TestCase):
    fixtures = ['app_name/fixtures/myfixture.json']

This thread shows up among the first results with a Google search "load data from all fixtures" and doesn't mention what IMO is the correct solution for this, ie the solution that allows you to load any fixtures you want without any wildcard tricks nor a single modification of the settings.py file (I also used to do it this way)

Just make your apps' fixtures directories flat (and not the usual Django scheme that e.g. goes app_name/templates/app_name/mytemplate.html), ie app_name/fixtures/myfixture.[json, yaml, xml]

Here's what the django doc says :

For example:

django-admin loaddata foo/bar/mydata.json

would search /fixtures/foo/bar/mydata.json for each installed application, /foo/bar/mydata.json for each directory in FIXTURE_DIRS, and the literal path foo/bar/mydata.json.

What that means is that if you have a fixtures/myfixture.json in all your app directories, you just have to run

./manage.py loaddata myfixture

to load all the fixtures that are located there within your project ... And that's it ! You can even restrict what apps you load fixtures from by using --app or --exclude arguments.

I'll mention that I use my fixtures only to populate my database while doing some development so I don't mind having a flat structure in my 'fixtures' directories ... But even if you use your fixtures for tests it seems like having a flat structure is the Django-esque way to go, and as
that answer suggests, you would reference the fixture from a specific app by just writing something like :

class MyTestCase(TestCase):
    fixtures = ['app_name/fixtures/myfixture.json']
云仙小弟2024-08-27 21:21:12

为什么不创建一个 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

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

Why not create a Makefile that pulls in all your fixtures? eg something like:

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

And then at the shell prompt, run

make load_all_fixtures

(This kind of approach is also good for executing unit tests for certain apps only and ignoring others, if need be)

乱了心跳2024-08-27 21:21:12

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

python manage.py loaddata a.json b.json c.json

My command is this, simple. (django 1.6)

python manage.py loaddata a.json b.json c.json
画▽骨i2024-08-27 21:21:12

如果你想在 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)

If you want to have this work on linux and windows you simply could use this for loading all your 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)
拒绝两难2024-08-27 21:21:12

适用于我的 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

Works for me with Django-admin version 3.1.4

python manage.py loaddata json_file_1 json_file_2

My folder structure is like this -

app_name_1
├──fixtures
├────json_file_1.json
├────json_file_2.json
app_name_2
├──fixtures
├────json_file_3.json
耀眼的星火2024-08-27 21:21:12

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

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

正如我在此[question][2]中所述,我也在 fabfile 中包含此内容。编辑:如果manage.py不在您的VE路径中,请使用python manage.py。

Manage.py loaddata will look automatically in certain places, so if you name your fixtures the same in each app, or put all your fixtures in the same folder it can be easy to load them all. If you have many different fixtures, and need a more complex naming schema, you can easily load all your fixtures using find with -exec

find . -name "*.json" -exec manage.py loaddata {} \;

As I state in this [question][2], I also have this in a fabfile. EDIT: use python manage.py if manage.py is not in your VE path.

や三分注定2024-08-27 21:21:12

如果您的装置位于同一文件夹中,您只需 lsxargsls 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

If your fixtures are located into the same folder, you can simply ls and xargs: ls myfolder | xargs django-admin loaddata.

Example with this structure:

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

would do the same as:

$ django-admin loaddata 1_users.json
$ django-admin loaddata 2_articles.json
$ django-admin loaddata 3_addresses.json
久随2024-08-27 21:21:12

经过一番搜索后,我最终编写了这个脚本。它在所有名为“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))

After doing a bit of searching, I ended up writing this script. It searches through all directories named "fixtures" for .json files and runs a "python manage.py loaddata {fixture_name}.json". Sometimes ordering matters for foreign key constraints, so it leaves a fixture in the queue if the constraint cannot be resolved.

(Note: It requires the pip package simple_terminal that I wrote. And I set it up to be run by 'python manage.py runscript ', which requires 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))
没有你我更好2024-08-27 21:21:12

这对我来说效果很好;它会查找位于 src 目录中的 fixtures 目录中的所有文件:

python manage.py loaddata \
    $(ls -1 src/**/fixtures/* | tr '\n' '\0' | xargs -0 -n 1 basename | tr '\n' ' ')

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' ' ')
叹沉浮2024-08-27 21:21:12
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
python manage.py loaddata ./*/fixtures/*.json

This command will look for the folder fixture in all the directory and then it will pick up all the files with json extension and will install the fixtures.

This way you won't have to have just one folder for fixtures instead you can have fixtures on app level and in multiple apps.

It will work with the ideal folder structure like this -

app_name_1
├──fixtures
├────json_file_1.json
├────json_file_2.json
app_name_2
├──fixtures
├────json_file_3.json
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文