Changes between Version 18 and Version 19 of ContribAuthImprovements

04/04/2012 01:08:29 AM (4 years ago)

Added Option 2c; generic swappable models.


  • 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
     175== Solution 2c: Generic swappable models ==
     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.
     179== Implementation ==
     181A model that wants to declare itself as swappable adds a new Meta option:
     184class User(Model):
     185    ....
     186    class Meta:
     187        swappable = 'user'
     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'.
     192We then introduce a `SWAPPABLE_MODELS` setting that provides a way for users to specify which models will be overridden in this application:
     196    'user': 'myapp.SuperDuperUser'
     199This specifies that the 'user' model will be satisfied by !SuperDuperUser in this project.
     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.
     203We then add a !LazyForeignKey():
     205class Comment(Model):
     206    user = LazyForeignKey('user')
     207    ....
     209that will resolve the !ForeignKey to the currently defined swappable model.
     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).
     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.
     215=== Advantages ===
     217As for Solution 2a, but:
     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.
     222=== Problems ===
     224As for Solution 2a.
    175226== Solution 3: Leverage App Refactor ==
Back to Top