Changes between Version 18 and Version 19 of ContribAuthImprovements


Ignore:
Timestamp:
Apr 4, 2012, 1:08:29 AM (13 years ago)
Author:
Russell Keith-Magee
Comment:

Added Option 2c; generic swappable models.

Legend:

Unmodified
Added
Removed
Modified
  • ContribAuthImprovements

    v18 v19  
    173173 * !ModelForm (and any other code that introspects auth.User) should be made lazy, or else circular dependencies can result. Introspecting auth.User and plugging into auth.User from the same app or an app loaded later is a potential circular dependency. (This is not a problem, unless you put mixins in models.py?)
    174174
     175== Solution 2c: Generic swappable models ==
     176
     177Follows the general direction of Solution 2a, but instead of a `USER_MODEL` setting that only solves the problem for auth.User, sets up the infrastructure for the general concept of swappable models.
     178
     179== Implementation ==
     180
     181A model that wants to declare itself as swappable adds a new Meta option:
     182
     183{{{
     184class User(Model):
     185    ....
     186    class Meta:
     187        swappable = 'user'
     188}}}
     189
     190Here, the name 'user' is an identifier that will be used to refer to this swappable model. By convention, it might be a good idea to namespace this tag (e.g., 'auth.user', instead of just 'user'.
     191
     192We then introduce a `SWAPPABLE_MODELS` setting that provides a way for users to specify which models will be overridden in this application:
     193
     194{{{
     195SWAPPABLE_MODELS = {
     196    'user': 'myapp.SuperDuperUser'
     197}
     198}}}
     199This specifies that the 'user' model will be satisfied by !SuperDuperUser in this project.
     200
     201If a model identifier (i.e., 'user') is mentioned in `SWAPPABLE_MODELS`, then the original model (auth.User) isn't synchronised to the database, and isn't added to the App cache.
     202
     203We then add a !LazyForeignKey():
     204{{{
     205class Comment(Model):
     206    user = LazyForeignKey('user')
     207    ....
     208}}}
     209that will resolve the !ForeignKey to the currently defined swappable model.
     210
     211If an app defines a model with a !ForeignKey to a swappable model, a warning is raised (since the model could potentially be swapped out); if the model is currently defined as swapped out, then an error will be raised (since the ForeignKey will be pointing at the wrong table).
     212
     213The app cache gains a registry of swappable models, and a `get_swappable_model(identifier)` entry point, so that users can easily retrieve the model currently being used as the swappable model for the various swappable endpoints.
     214
     215=== Advantages ===
     216
     217As for Solution 2a, but:
     218
     219 * Solves the general problem of swappable models, so other apps with similar problems (e.g., comments) can use the same infrastructure instead of having to reinvent the wheel.
     220 * Catches migration problems as warnings/errors if existing apps haven't been updated to point to the swappable model.
     221
     222=== Problems ===
     223
     224As for Solution 2a.
     225
    175226== Solution 3: Leverage App Refactor ==
    176227
Back to Top