32

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?

Paolo
  • 20,112
  • 21
  • 72
  • 113
frnhr
  • 12,354
  • 9
  • 63
  • 90

2 Answers2

29

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.

Paolo
  • 20,112
  • 21
  • 72
  • 113
Sarah Messer
  • 3,592
  • 1
  • 26
  • 43
2

Django 1.5 added the swappable user models, so you can replace Django User model with one of your own (limit the number of characters, use email as identifier...)

More: https://docs.djangoproject.com/en/dev/releases/1.5/#configurable-user-model https://docs.djangoproject.com/en/dev/topics/auth/customizing/#auth-custom-user

fasouto
  • 4,386
  • 3
  • 30
  • 66
  • 3
    Sorry, I should have been more clear in the question. Yes, I guessed what is's for. My question is how does it actually pull that off? – frnhr Feb 25 '14 at 21:04