Swappable Models

Created on Nov. 20, 2012, 11:03 a.m. by Hevok & updated by Hevok on May 2, 2013, 5:27 p.m.

Django 1.5 adds the ability to add a swappable User models, which means that it is possible to replace Django's builtin User model with any other defined User model (for instance a User model that uses email as the unique identifier, or one that captures API credentials rather than first and last name.

In theory any model can be declared as swappable, which will all to define that in a certain project the "X" model will be performed by model "Y", and any foreign keys will be re-routed appropriately.

To do this, on the model that has to be swappable (e.g. a User model), add a Meta declaration:

class Meta:
    swappable = 'CUSTOM_USER_MODEL'

When the database in synchronized, syncdb will look for the setting called 'CUSTOM_USER_MODEL' (or anything else for USER, e.g. PEOPLE); if it exists, it will not sync User into the database, and will replace any references to User with reference to the model defined in CUSTOM_USER_MODEL.


Tags: users, rest, django
Categories: Tutorial, reST
Parent: Web Framework

Update entry (Admin) | See changes

Comment on This Data Unit