[Fixed]-No fixture named 'X' found

9👍

South and fixtures

South simply patches syncdb to skip the fixture loading for models with migrations, and actually loads them after the final migration for an app has been run.

Make sure your initial_data file is located in the correct place

Loading initial_data does not require you do actually do something, but place the fixtures in the correct place as explained in Django’s documentation.

To quote the docs:

By default, Django looks in the fixtures directory inside each app for
fixtures. You can set the FIXTURE_DIRS setting to a list of additional
directories where Django should look.

This means that if you have an app called “myapp”, you’d create a “fixtures” dir inside it and place the json there, e.g.: myproject/myapp/fixtures.

Django 1.7 and newer

Django 1.7 introduced built-in migrations. These have an interface similar to South; management commands to create migrations makemigrations, run them migrate, and others.

However, initial_data fixtures are no longer auto-loaded on syncdb run; unless it is an existing app, and has no migrations. This is mentioned in the release notes.

The docs now recomend to create a datamigration to handle fixture loading. Luckily, this is pretty easy to do, here’s how I usually do it:

1. create an empty data migration

$ python manage.py makemigrations --empty myapp

If you had only the initial migration, you end up with these files (note that I renamed migration 0002 for clarity):

myapp/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ fixtures
β”‚Β Β  └── my_initial_data.json
└── migrations
 Β Β  β”œβ”€β”€ 0001_initial.py
 Β Β  β”œβ”€β”€ 0002_load_fixture.py
    └── __init__.py

2. update the code of 0002_load_fixture.py to run loaddata

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations
from django.core.management import call_command


def load_my_initial_data(apps, schema_editor):
    call_command("loaddata", "my_initial_data.json")


class Migration(migrations.Migration):
    dependencies = [
        ('myapp', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(load_my_initial_data),
    ]

4👍

This error also occurs when the filename does not include the ‘.json’ extension.

In order to load a fixture from current working directory it has to end with ‘.json’.

👤pkk

0👍

Why don’t you run loaddata comand from your migration then?

import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models

class Migration(DataMigration):

    def forwards(self, orm):
        from django.core.management import call_command
        call_command("loaddata", "my_fixture.json")
👤aabele

0👍

If your still getting the error:

No fixture named ‘X’ found

Try using the path to the json file, as manage.py is probably not being run from the project directory (it may even be run from the root directory). Try something like this:

import os

app_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))
json_path = os.path.join(app_path, 'fixtures', 'my_fixture.json')
call_command("loaddata", json_path)

Leave a comment