DJANGO 1.11 - 找不到固定装置

标签 django django-fixtures

问题

我试图使用 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

最佳答案

您是否应该在设置文件中定义 FIXTURE_DIRS,以便 Django 找到您的灯具。

settings.py

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

Reference

关于DJANGO 1.11 - 找不到固定装置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54510283/

相关文章:

django-fixtures - Django Sites Framework:初始数据迁移位置

python - Django - 过滤器不返回数据库中的对象

python - DRF API 端点获取 "Authentication credentials were not provided"

django - 从数据装置更新数据库记录

symfony - 如何在 Symfony 中两个表之间存在关系的地方创建夹具?

django - 如何使用manage.py dumpdata创建更漂亮的装置?

python - 在Django中的特定时间执行任务

python - Django Admin 仅按反向外键查找中的第一个对象按功能/过滤器过滤

javascript - 使用 Ajax 更新特定对象中单个字段的正确方法

python - Django 测试用例,可以在类/模块级别加载夹具吗?