Changes between Initial Version and Version 1 of ExtendedUserModel


Ignore:
Timestamp:
Dec 27, 2005, 2:48:33 AM (18 years ago)
Author:
jim-django@…
Comment:

Created page.

Legend:

Unmodified
Added
Removed
Modified
  • ExtendedUserModel

    v1 v1  
     1On the Django IRC channel, I was helped through some resources for replacing the auth systems's User model with my own extended version. Here are the notes I made. Note that it was 2:30 am local time, beware hacker fatigue syndrome.
     2
     3
     4{{{
     5[02:18] <harmless> phew, that was NOT easy
     6[02:18] <harmless> note to all:
     7[02:18] <harmless> if you wish to replace the auth User model with an extended version, you must do the following:
     8[02:19] <harmless> 1. patch the createsuperuser function in core/management.py to catch ObjectDoesNotExist rather than users.UserDoesNotExist
     9[02:20] <harmless> 2. ensure that your model's META specifies module_name = "users"
     10[02:21] <harmless> 3. provide a _module_create_user function that does all of what django.models.auth.User's does, plus inits your new fields
     11[02:21] <harmless> 3 a. note that it's easier to use keyword arguments here, as it's not clear what the field order is when extending a model
     12[02:21] <harmless> 4. wipe your entire database every time you make a change.
     13[02:22] <harmless> and that pretty much did it for me, i've got my own user class being used successfully for auth and admin, and it's a single nice class in the admin interface.
     14[02:22] <harmless> so maybe next time i need to do this, this transcript will be a quick google search away :)
     15[02:25] <alect> perhaps you should wikify it
     16[02:35] <paulproteus|lapt> I'm with alect on this one. (-:
     17[02:36] <harmless> well, i'm not sure it's wikiworthy at this point
     18[02:36] <harmless> it looks like it's changing right away anyways
     19[02:36] <harmless> but i'll paste it in with a "late night hacking" warning
     20}}}
     21
     22Missing here is step 2b, ensure that your model's META specified replaces_module="auth.users",
     23and step 5, you must copy the original models' entire META.admin section if you wish to change it at all.
     24
     25Here's what my model looks like at this point:
     26
     27
     28{{{
     29class User(users.User):   
     30  gamename = meta.CharField("Game Name", maxlength=100, blank=True)
     31  url = meta.CharField(maxlength=100, blank=True)
     32  description = meta.TextField(blank=True)   
     33  # SNIP a few more extra model attributes
     34
     35  class META:   
     36    replaces_module = 'auth.users'
     37    module_name = "users"
     38    verbose_name = _('User')
     39    verbose_name_plural = _('Users')
     40    module_constants = {
     41      'SESSION_KEY': '_auth_user_id',
     42    }
     43    ordering = ('username',)
     44    exceptions = ('SiteProfileNotAvailable',)
     45   
     46    admin = meta.Admin(
     47      fields = (
     48        (None, {'fields': ('username', 'password_md5')}),
     49        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
     50        (_('Extended info'), {'fields': ('gamename', 'url', 'description')}),
     51        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
     52        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
     53        (_('Groups'), {'fields': ('groups',)}),
     54      ),
     55      list_display = ('username', 'gamename', 'first_name', 'last_name', 'is_staff'),
     56      list_filter = ('is_staff', 'is_superuser'),
     57      search_fields = ('username', 'first_name', 'last_name', 'email',
     58                       'gamename', 'description'),
     59    )
     60
     61
     62}}}
Back to Top