Understanding Fixtures in Django: Creation, Loading, Discovery, Ordering, and Best Practices

This article explains what fixtures are in Django, how to create and load them, how Django discovers fixture files, how fixture ordering works, how data is saved during fixture loading, how to safely handle signals, how compressed fixtures behave, and how to use database‑specific fixtures in multi‑database environments.

fixtures, loaddata, dumpdata, FIXTURE_DIRS, TestCase fixturescompressed fixtures, raw=TrueDjango serialization

~3 min read • Updated Mar 10, 2026

1. What Are Fixtures?

A fixture is a collection of files containing serialized data for populating the database. Each fixture has a unique name and may consist of multiple files spread across different directories or applications.

Fixtures are commonly used for:

  • Providing initial data
  • Populating test databases
  • Sharing reusable datasets across apps

2. How to Create a Fixture

The easiest way to generate a fixture is using dumpdata:


python manage.py dumpdata app.Model --indent 2 > mydata.json

You can also create fixtures manually or by using Django’s serialization tools.

3. How to Use a Fixture

3.1 In Tests


class MyTestCase(TestCase):
    fixtures = ["fixture-label"]

3.2 Using loaddata


django-admin loaddata 

4. How Django Discovers Fixtures

Django searches for fixtures in the following locations:

  • fixtures/ directory of each installed app
  • Directories listed in FIXTURE_DIRS
  • The literal path provided

4.1 With file extension


django-admin loaddata mydata.json

Loads only JSON fixtures named mydata.

4.2 Without file extension


django-admin loaddata mydata

Django searches all registered serializers (json, xml, yaml, etc.).

4.3 Fixtures with directory components


django-admin loaddata foo/bar/mydata.json

Django searches:

  • <app>/fixtures/foo/bar/mydata.json
  • <dir in FIXTURE_DIRS>/foo/bar/mydata.json
  • The literal path

5. Fixture Loading Order

When multiple fixtures are listed, Django loads them in the order provided:


django-admin loaddata mammals birds insects

Or in tests:


class AnimalTestCase(TestCase):
    fixtures = ["mammals", "birds", "insects"]

Order matters, especially when foreign key relationships exist.

Note: Some databases (e.g., MySQL without deferred constraints) may fail if relationships span fixtures.

6. How Fixtures Are Saved to the Database

When loading fixtures:

  • Model.save() is not called
  • Signals are triggered with raw=True
  • Only fields present in the fixture are populated

6.1 Disabling signals during fixture loading


def my_handler(**kwargs):
    if kwargs["raw"]:
        return
    ...

6.2 Using a decorator


from functools import wraps

def disable_for_loaddata(handler):
    @wraps(handler)
    def wrapper(*args, **kwargs):
        if kwargs["raw"]:
            return
        return handler(*args, **kwargs)
    return wrapper

@disable_for_loaddata
def my_handler(**kwargs):
    ...

This disables the handler whenever fixtures are deserialized.

7. Compressed Fixtures

Django supports fixtures compressed with:

  • zip
  • gz
  • bz2
  • lzma
  • xz

For example:


django-admin loaddata mydata.json

Django will look for:

  • mydata.json
  • mydata.json.zip
  • mydata.json.gz
  • mydata.json.bz2
  • mydata.json.lzma
  • mydata.json.xz

Important: If two fixtures with the same name but different formats exist, Django aborts loading and rolls back.

8. MySQL with MyISAM

MyISAM does not support transactions or constraints. Therefore:

  • No validation of fixture data
  • No rollback if multiple fixture files cause errors

9. Database-Specific Fixtures

In multi-database setups, you can target fixtures to a specific database by naming them accordingly.

Example:


mydata.users.json
mydata.users.json.gz

These fixtures load only when you run:


django-admin loaddata mydata --database=users

Conclusion

Fixtures are a powerful tool for loading structured data into Django applications. Understanding how they are created, discovered, ordered, and loaded—along with how signals behave and how to use compressed or database‑specific fixtures—helps ensure smooth and predictable data loading workflows.

Written & researched by Dr. Shahin Siami