forked from grnet/djnro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dj1.8: repl longerusername with custom user model
The longerusername package is not really compatible with Django 1.7+ - it only hooks into South migrations, and Django 1.7+ does not allow the "monkeypatch" approach that longerusername uses to modify the Auth.User model. Instead, do the proper thing and define a custom user model as per https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#specifying-a-custom-user-model To make the transition easy: * Create a model that inherits from Auth.AbstractUser, only modifying the username length * Make the model use the auth_user DB table so it picks up existing data * Call the model User so that links from other existing tables still work For existing Django 1.4 databases, the model would just inherit the existing table structure. For new Django 1.8 databases, the generated initial migrations would create the tables the same way. Make the following considerations in designing and implementing the model: * Register the model (accounts.User) with the admin class (django.contrib.auth.admin.UserAdmin) in accounts/admin.py, not accounts/models.py (follow conventions in django.contrib.auth and resolve compatibility issues raised in grnet#15). * Mark the model as swappable. * Make sure the migration also keeps the swappable property. * Update imports across the DjNRO codebase to reflect that User is now defined in accounts.models, not django.contrib.auth.models - and if possible, in model definition, refer to the (swappable) user model through settings.AUTH_USER_MODEL. Swappable models: The accounts.User Meta class has the same property "swappable" to as is defined in auth.User: class User(AbstractUser): ... class Meta(AbstractUser.Meta): swappable = 'AUTH_USER_MODEL' The value of the property is the name of a setting from the Django project's settings that defines which model class implements a particular feature (the user model in this case). With this feature on, only the model class where the name of the class matches the value of the setting referenced by the property is active. Without adding this property, and when changing settings.AUTH_USER_MODEL back to auth.User (either explicitly or by removing the setting and letting it default to this value), the accounts.User model class would clash with auth.User - they'd be both adding foreign key relations with the same name to related classes.
- Loading branch information
1 parent
3c341d4
commit 7acf827
Showing
11 changed files
with
38 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from django.contrib import admin | ||
from accounts.models import UserProfile | ||
|
||
from accounts.models import User, UserProfile | ||
from django.contrib.auth.admin import UserAdmin | ||
|
||
class UserPrAdmin(admin.ModelAdmin): | ||
list_display = ('user', 'institution') | ||
|
||
admin.site.register(UserProfile, UserPrAdmin) | ||
admin.site.register(User, UserAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters