【问题标题】:Load Multiple Fixtures at Once一次加载多个灯具
【发布时间】:2011-01-14 13:37:04
【问题描述】:

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

理想情况下我想输入:

python manage.py loaddata all_fixtures

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

【问题讨论】:

  • 你能给我们举个例子,看看你的fixture目录是什么样的,更具体地说,里面有什么文件?

标签: django django-fixtures


【解决方案1】:

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

【讨论】:

  • 在 Mac OS X 上我得到:“UserWarning: No fixture named '*' found.”
  • 有什么办法可以按特定顺序完成吗?
  • 它正在工作。 $ python manage.py loaddata folder_name/*.json
【解决方案2】:

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

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

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

ls */fixtures/*.json

【讨论】:

  • 适用于 macOS
  • 这可行,但是,就我而言,我必须确保以特定顺序(依赖项)加载固定装置。所以,我一个接一个地加载它们。
【解决方案3】:

为什么不创建一个包含所有装置的 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

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

【讨论】:

  • 上面的 sn-p 是您在 Makefile 中需要的内容
  • make: 'load_all_fixtures' 无事可做。收到此错误
【解决方案4】:

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

只要让你的应用的fixtures目录扁平化(而不是通常的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,为 FIXTURE_DIRS 中的每个目录搜索 /foo/bar/mydata.json,以及文字路径 foo/bar/mydata.json。

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

./manage.py loaddata myfixture

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

我会提到,我在进行一些开发时只使用我的固定装置来填充我的数据库,所以我不介意在我的“固定装置”目录中有一个平面结构......但即使你使用固定装置来测试它似乎有一个扁平的结构是 Django 式的方式,并且作为 that answer 建议,您可以通过编写类似以下内容来引用特定应用程序中的夹具:

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

【讨论】:

    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

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

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

      【讨论】:

      • 这是最简单的解决方案。 python manage.py loaddata armor_fixture enemies_fixture player_fixture room_fixture roomgen_fixture weapon_fixture 为我工作
      【解决方案7】:

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

      【讨论】:

        【解决方案8】:

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

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

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

        【讨论】:

          【解决方案9】:

          如果您的灯具位于同一个文件夹中,您可以简单地 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
          

          【讨论】:

          • 如果我们需要users.id来填写文章或地址,我认为序列号至关重要。谢谢!
          【解决方案10】:

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

          【讨论】:

            【解决方案11】:

            这对我很有效;它会在src 目录中找到位于fixtures 目录中的所有文件:

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

            【讨论】:

              猜你喜欢
              • 2011-12-31
              • 2011-04-15
              • 1970-01-01
              • 2010-09-18
              • 1970-01-01
              • 1970-01-01
              • 2012-01-20
              • 2018-06-28
              • 1970-01-01
              相关资源
              最近更新 更多