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

Related Articles

Django Tasks Framework: A Complete Guide to Background Task Execution in Django 6.0

Django 6.0 introduces the Tasks framework, a built‑in system for defining and queuing background work outside the request–response cycle. This article explains how Tasks work, how to configure backends, how to define and enqueue tasks, how context works, and how to integrate third‑party worker systems for production environments.

Continue

Asynchronous Support in Django: A Complete Guide to Async Views, ORM, Middleware, Performance, and Safety

This article explains Django’s asynchronous (async) capabilities, including async views, ASGI support, middleware behavior, async ORM features, performance considerations, handling disconnects, and Django’s async safety protections. It also covers how to use sync_to_async(), async ORM methods, and how to safely run synchronous code in async environments.

Continue

Django System Check Framework: A Complete Guide to Writing, Registering, Running, and Testing System Checks

This article explains Django’s System Check Framework—a powerful mechanism for detecting configuration issues, validating project structure, and ensuring code quality. It covers how checks are executed, how to write custom checks, how messages work, how to register and tag checks, how to extend checks for fields and models, and how to write both unit and integration tests for system checks.

Continue

Django Signals: A Complete Guide to Listening, Connecting, Sending, and Managing Application Events

This article provides a comprehensive explanation of Django’s signal system—an event‑driven mechanism that allows decoupled applications to react to actions occurring elsewhere in the framework. It covers how to define receivers, connect signals, use decorators, handle specific senders, organize signal code, and follow best practices to avoid complexity.

Continue

Understanding Django Settings: Configuration, Environment Management, and Best Practices

This article provides a complete overview of Django’s settings system. It explains how settings files work, how to designate a settings module, how to use settings in your code, how to configure Django manually, how to secure sensitive settings, and how to work with custom default settings. It also covers the role of django.setup() for standalone scripts.

Continue

Serializing and Deserializing Django Objects: A Complete Guide to Django’s Serialization Framework

This article explains Django’s serialization framework, including how to serialize and deserialize model instances, work with subsets of fields, handle inherited models, use different serialization formats (JSON, XML, YAML, JSONL), and understand how relational fields are represented. It also covers DeserializedObject behavior and common pitfalls.

Continue