views:

456

answers:

2

I'm attempting to extend django's contrib.auth User model, using an inline 'Profile' model to include extra fields.

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

class Profile(models.Model):

  user = models.ForeignKey(User, unique=True, related_name='profile')
  avatar = '/images/avatar.png'
  nickname = 'Renz'

class UserProfileInline(admin.StackedInline):

  model = Profile

class UserProfileAdmin(UserAdmin):

  inlines = (UserProfileInline,)

admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)

This works just fine for the admin 'Change User' page, but I can't find a way to add inline model fields in the list_display. Just specifying the names of Profile fields in list_display give me an error:

UserProfileAdmin.list_display[4], 'avatar' is not a callable or an attribute of 'UserProfileAdmin' or found in the model 'User'.

I can create a callable which looks up the user in the Profile table and returns the relevant field, but this leaves me without the ability to sort the list view by the inline fields, which I really need to be able to do.

Any suggestions?

+3  A: 

You've mentioned the only solution - creating a callable. There's currently no other way to do it, and yes this does mean you can't sort by that column.

Daniel Roseman
A: 

Thank you for this snippet above, I was just trying to figure out how to do add an inline field for the User Table

Ed Johnstone