Tiago Martins
Tiago Martins

Reputation: 175

DJANGO 1.11 - Can't find fixtures

Problem

I was trying to use fixtures to populate my database, so I started by reading through the documentation for loaddata and by default, it looks in the fixtures directory inside each app for fixtures. My problem is that when I run python manage.py loaddata I'm getting an error, but when I give it the path for teams.yaml it works fine. Do I need to setup something for it to work?

Documentation

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

Error

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

Model

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'), 
        )

Fixture (teams.yaml)

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

Upvotes: 3

Views: 1870

Answers (2)

Khashayar Ghamati
Khashayar Ghamati

Reputation: 368

Should you define FIXTURE_DIRS in your settings file so django find your fixtures which there are there.

settings.py

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

Reference

Upvotes: 1

bgsuello
bgsuello

Reputation: 702

The error says: No database fixture specified. Please provide the path of at least one fixture in the command line.

You need to provide a fixturename in loaddata command, in your case:

python manage.py loaddata team

It was specified in the doc that by default Django will look into fixtures directory inside your app with the specified fixturename. Also the command accepts ./path/to/fixtures/ which overrides the searching of fixtures directory.

Upvotes: 0

Related Questions