【问题标题】:DJANGO 1.11 - Can't find fixturesDJANGO 1.11 - 找不到固定装置
【发布时间】:2019-06-27 20:20:51
【问题描述】:

问题

我试图使用fixtures 来填充我的数据库,因此我首先阅读了loaddata 的文档,默认情况下,它会在每个应用程序内的fixtures 目录中查找fixtures。我的问题是,当我运行 python manage.py loaddata 时出现错误,但是当我给它提供 teams.yaml 的路径时,它工作正常。我需要设置一些东西才能让它工作吗?

文档

https://docs.djangoproject.com/en/1.11/howto/initial-data/#where-django-finds-fixture-files

错误

usage: manage.py loaddata [-h] [--version] [-v {0,1,2,3}]
                          [--settings SETTINGS] [--pythonpath PYTHONPATH]
                          [--traceback] [--no-color] [--database DATABASE]
                          [--app APP_LABEL] [--ignorenonexistent] [-e EXCLUDE]
                          fixture [fixture ...]
manage.py loaddata: error: No database fixture specified. Please provide the path of at least one fixture in the command line.

团队应用目录

team
├── admin.py
├── apps.py
├── fixtures
│   └── teams.yaml
├── __init__.py
├── migrations
│   ├── 0001_initial.py
│   ├── 0002_auto_20190204_0438.py
│   ├── 0003_remove_team_team_name.py
│   ├── `enter code here`0004_team_team_name.py
│   ├── __init__.py
│   └── __pycache__
│       ├── 0001_initial.cpython-36.pyc
│       ├── 0002_auto_20190204_0438.cpython-36.pyc
│       ├── 0003_remove_team_team_name.cpython-36.pyc
│       ├── 0004_team_team_name.cpython-36.pyc
│       └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│   ├── admin.cpython-36.pyc
│   ├── apps.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   └── models.cpython-36.pyc
├── tests.py
└── views.py

型号

from django.db import models

class Team(models.Model):
    team_name = models.CharField(max_length=32)
    team_description = models.TextField(max_length=512, blank=False)

    class Meta:
        permissions = (
            ('create_team', 'Can create a team'), 
        )

夹具(teams.yaml)

- model: team.Team
  pk: 1
  fields:
    team_name: team_name_example
    team_descrition: team_description_example

【问题讨论】:

    标签: django django-fixtures


    【解决方案1】:

    错误说: No database fixture specified. Please provide the path of at least one fixture in the command line.

    您需要在loaddata 命令中提供fixturename,在您的情况下:

    python manage.py loaddata team
    

    在文档中指定默认情况下 Django 将使用指定的 fixturename 查看应用程序内的固定装置目录。该命令还接受./path/to/fixtures/,它覆盖了对fixtures 目录的搜索。

    【讨论】:

    • 哦,谢谢,我误解了文档。你知道如何让它搜索所有应用程序中的所有灯具吗?
    • 你的意思是,加载所有应用程序中的所有灯具?
    • 是的,我想加载所有应用程序中的所有灯具
    • 这正是我想要的。很抱歉浪费了你的时间,我仍然是谷歌搜索的菜鸟。感谢您的耐心等待。
    【解决方案2】:

    您是否应该在设置文件中定义FIXTURE_DIRS,以便 django 找到您的固定装置。

    settings.py

    FIXTURE_DIRS = [
        os.path.join(BASE_DIR, 'fixtures')
    ]
    # statements
    

    Reference

    【讨论】:

    • 我遇到了同样的错误。 @bgsuello 评论的链接让我得到了我需要的东西。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2018-04-09
    • 2017-12-23
    • 2012-10-07
    • 2011-04-29
    • 2013-09-02
    相关资源
    最近更新 更多