DJANGO 1.11-找不到装置

时间:2019-02-04 04:49:50

标签: django django-fixtures

问题

我试图使用Fixture填充数据库,所以我首先阅读了loaddata文档,默认情况下,它在每个应用程序内的Fixtures目录中查找Fixture。我的问题是,当我运行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 App Dir

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

2 个答案:

答案 0 :(得分:0)

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

对于您的情况,您需要在fixturename命令中提供一个loaddata

python manage.py loaddata team

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

答案 1 :(得分:0)

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

settings.py

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

Reference