Skip to main content

Swapper

Dependency monitoring

Django Swappable Models - No longer only for auth.User!

Swapper is an unofficial API for the undocumented but very powerful Django feature: swappable models. Swapper facilitates implementing arbitrary swappable models in your own reusable apps.

Build Status Latest PyPI Release Release Notes License GitHub Stars GitHub Forks

Requirements

  • Python 3.10 or later
  • Django 4.2 or later

Motivation

Suppose your reusable app has two related tables:

from django.db import models
class Parent(models.Model):
    name = models.TextField()

class Child(models.Model):
    name = models.TextField()
    parent = models.ForeignKey(Parent, on_delete=models.CASCADE)

Suppose further that you want to allow the user to subclass either or both of these models and supplement them with their own additional fields. You could use Abstract classes (e.g. BaseParent and BaseChild) for this, but then you would either need to:

  1. Avoid putting the foreign key on BaseChild and tell the user they need to do it.
  2. Put the foreign key on BaseChild, but make Parent a concrete model that can't be swapped
  3. Use swappable models, together with ForeignKeys that read the swappable settings.

This third approach is taken by Django to facilitate swapping the auth.User model. The auth.User swappable code was implemented in a generic way that allows it to be used for any model. Although this capability is currently undocumented while any remaining issues are being sorted out, it has proven to be very stable and useful in our experience.

Swapper is essentially a simple API wrapper around this existing functionality. Note that Swapper is primarily a tool for library authors; users of your reusable app generally should not need to know about Swapper in order to use it. (See the notes on End User Documentation below.)

Real-World Examples

Swapper is used extensively in several OpenWISP packages to facilitate customization and extension. Notable examples include:

The use of swapper in these packages promotes Software Reusability, one of the core values of the OpenWISP project.

Creating a Reusable App

First, make sure you have swapper installed. If you are publishing your reusable app as a Python package, be sure to add swapper to your project's dependencies (e.g. setup.py) to ensure that users of your app don't have errors integrating it.

pip3 install swapper

Extending the above example, you might create two abstract base classes and corresponding default implementations:

# reusableapp/models.py
from django.db import models
import swapper

class BaseParent(models.Model):
    # minimal base implementation ...
    class Meta:
        abstract = True

class Parent(BaseParent):
    # default (swappable) implementation ...
    class Meta:
       swappable = swapper.swappable_setting('reusableapp', 'Parent')

class BaseChild(models.Model):
    parent = models.ForeignKey(
        swapper.get_model_name('reusableapp', 'Parent'), on_delete=models.CASCADE
    )
    # minimal base implementation ...
    class Meta:
        abstract = True

class Child(BaseChild):
    # default (swappable) implementation ...
    class Meta:
       swappable = swapper.swappable_setting('reusableapp', 'Child')

Loading Swapped Models

In your reusable views and other functions, always use the swapper instead of importing swappable models directly. This is because you might not know whether the user of your app is using your default implementation or their own version.

# reusableapp/views.py

# Might work, might not
# from .models import Parent

import swapper
Parent = swapper.load_model("reusableapp", "Parent")
Child = swapper.load_model("reusableapp", "Child")

def view(request, *args, **kwargs):
    qs = Parent.objects.all()
    # ...

Note: swapper.load_model() is the general equivalent of get_user_model() and subject to the same constraints: e.g. it should not be used until after the model system has fully initialized.

Migration Scripts

Swapper can also be used in migration scripts to facilitate dependency ordering and foreign key references. To use this feature in your library, generate a migration script with makemigrations and make the following changes. In general, users of your library should not need to make any similar changes to their own migration scripts. The one exception is if you have multiple levels of swappable models with foreign keys pointing to each other.

  # reusableapp/migrations/0001_initial.py

  from django.db import models, migrations
< from django.conf import settings
> import swapper

  class Migration(migrations.Migration):

      dependencies = [
<          migrations.swappable_dependency(settings.REUSABLEAPP_PARENT_MODEL),
>          swapper.dependency('reusableapp', 'Parent')
      ]

      operations = [
          migrations.CreateModel(
              name='Child',
              fields=[
                  ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),
              ],
              options={
<                 'swappable': 'REUSABLEAPP_CHILD_MODEL',
>                 'swappable': swapper.swappable_setting('reusableapp', 'Child'),
              },
              bases=(models.Model,),
          ),
          migrations.CreateModel(
              name='Parent',
              fields=[
                  ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),
              ],
              options={
<                 'swappable': 'REUSABLEAPP_PARENT_MODEL',
>                 'swappable': swapper.swappable_setting('reusableapp', 'Parent'),
              },
              bases=(models.Model,),
          ),
          migrations.AddField(
              model_name='child',
              name='parent',
<             field=models.ForeignKey(to=settings.REUSABLEAPP_PARENT_MODEL, on_delete=models.CASCADE),
>             field=models.ForeignKey(to=swapper.get_model_name('reusableapp', 'Parent'), on_delete=models.CASCADE),
              preserve_default=True,
          ),
      ]

End User Documentation

With the above setup, the user of your app can override one or both models in their own app. You might provide them with an example like this:

# myapp/models.py
from reusableapp.models import BaseParent
class Parent(BaseParent):
    # custom implementation ...

Then, tell your users to update their settings to trigger the swap.

# myproject/settings.py
REUSABLEAPP_PARENT_MODEL = "myapp.Parent"

The goal is to make this process just as easy for your end user as swapping the auth.User model is. As with auth.User, there are some important caveats that you may want to inform your users about.

The biggest issue is that your users will probably need to define the swapped model settings before creating any migrations for their implementation of myapp. Due to key assumptions made within Django's migration infrastructure, it is difficult to start out with a default (non-swapped) model and then later to switch to a swapped implementation without doing some migration hacking. This is somewhat awkward - as your users will most likely want to try out your default implementation before deciding to customize it. Unfortunately, there isn't an easy workaround due to how the swappable setting is currently implemented in Django core. This will likely be addressed in future Django versions (see #10 and Django ticket #25313).

API Documentation

Here is the full API for swapper, which you may find useful in creating your reusable app code. End users of your library should generally not need to reference this API.

function purpose
swappable_setting(app_label, model) Generates a swappable setting name for the provided model (e.g. "REUSABLEAPP_PARENT_MODEL")
is_swapped(app_label, model) Determines whether or not a given model has been swapped. (Returns the model name if swapped, otherwise False)
get_model_name(app_label, model) Gets the name of the model the swappable model has been swapped for (or the name of the original model if not swapped.)
get_model_names(app_label, models) Match a list of model names to their swapped versions. All of the models should be from the same app (though their swapped versions need not be).
load_model(app_label, model, required=True) Load the swapped model class for a swappable model (or the original model if it hasn't been swapped). If your code can function without the specified model, set required = False.
dependency(app_label, model, version=None) Generate a dependency tuple for use in migrations. Use version only when depending on the first migration of the target dependency doesn't work (eg: when a specific migration needs to be depended upon), we recommend avoid using version='__latest__' because it can have serious drawbacks when new migrations are added to the module which is being depended upon.
set_app_prefix(app_label, prefix) Set a custom prefix for swappable settings (the default is the upper case app_label). This can be useful if the app has a long name or is part of a larger framework. This should be set at the top of your models.py.
join(app_label, model), split(model) Utilities for splitting and joining "app.Model" strings and ("app", "Model") tuples.

Metadata

Release files for swapper 1.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swapper 1.5.0
File Size Uploaded
swapper-1.5.0.tar.gz 24.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for swapper 1.5.0
File Interpreter ABI Platform
swapper-1.5.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 31.4 kB

Release files / swapper-1.5.0.tar.gz

Download URL swapper-1.5.0.tar.gz
Size 24.1 kB
Tags Source
SHA-256 checksum
How to use checksums
405f934265aa0c8d8b8e6b1be584875d1a3b7de6b1cd6f47367a4dbdc028609a
BLAKE2b-256 checksum
How to use checksums
c23ea28d29aa28570b09257dad52cc5e5ebda9fe8802eeca0d9e254ccf8e5788
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.

Transparency log

Release files / swapper-1.5.0-py2.py3-none-any.whl

Download URL swapper-1.5.0-py2.py3-none-any.whl
Size 7.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
67ae5a01bc12159dede567b98212a83baa53aa466a8eb5cf44af226b8219d21c
BLAKE2b-256 checksum
How to use checksums
295feab0a39f9c16c5d32c00f1f9b7ee9e8823686d1fd40a7fd1f0fc3247a59c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.5.0 This release

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

1 release file

1.0.0

1 release file

0.3.0

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page