What is "swappable" in model meta for?

frnhr picture frnhr · Feb 25, 2014 · Viewed 8.7k times · Source

Looking tough django auth models code, I came across this bit of code:

class User(AbstractUser):
    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'

It's obvious that it has something to do with the new AUTH_USER_MODEL setting in settings.py, but how does it actually work, by what python "trick"?

And in what other situations can it be used?

Answer

Sarah Messer picture Sarah Messer · Jul 23, 2014

swappable is an "intentionally undocumented" feature which is currently under development / in-test. It's used to handle "I have a base abstract model which has some foreign-key relationships." Slightly more detail is available from Django's ticketing system and github. Because it's a "stealth alpha" feature, it's not guaranteed to work (for anything other than User), and understanding the detailed operation will likely require diving into source code. It works with AUTH_USER_MODEL because the User model and swappable flag were developed together, specifically for each other.