1

I was given a task to add a field "location" to the django prebuilt user module I found the location of the module itself and added the location field as well as the admin modules

eg:

class UserAdmin(admin.ModelAdmin):
    add_form_template = 'admin/auth/user/add_form.html'
    change_user_password_template = None
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'location')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )

I rebuilt the database hoping it would show up in the adding of new user form. But It didn't seem to work. So I assume I forgot to change something else. I was looking around in the html code for ages trying to find the location of where the actual "change user" form is called, but couldn't find it.

I would appreciate help from someone who dealt with the pre built django admin backend before. Thank you!

Angie
  • 313
  • 1
  • 8
  • 20

2 Answers2

1

It's not recommended to modify django's user model because it complicates things. Instead, you can add a user profile as documented here.

fahhem
  • 466
  • 4
  • 8
  • I have a question though. If i need the admin when creating a new user to specify his location in the creating new user form. How can I ask for this field to be filled out? I found this earlier question which is exactly the same as mine http://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django – Angie Jul 28 '11 at 08:17
1

If you want the profile to show up in the admin side, you can attach the profile as an inline and force it by unregistering the user and then re-registering it. Here's how I do it:

from django.contrib.auth.models import User
from reversion.admin import VersionAdmin
from profiles.models import Profile
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

class ProfileInline(admin.StackedInline):
    model = Profile
    fk_name = 'user'
    extra = 0
    max_num = 1
    fieldsets = [
        ('Company', {'fields': ['company', 'office']}),
        ('User Information', {'fields': ['role', 'gender', 'phone', 'mobile', 'fax', 'street', 'street2', 'city', 'state', 'zip']}),
        ('Site Information', {'fields': ['sites']}),
    ]

class NewUserAdmin(VersionAdmin):
    inlines = [ProfileInline, ]

admin.site.unregister(User)
admin.site.register(User, NewUserAdmin)
Luke
  • 558
  • 1
  • 5
  • 24