views:

1197

answers:

3

Heyas

I'm trying to understand the django admin better and at the same time, I'm trying to add one more field to the current user admin. In models.py I've done

User.add_to_class('new_field', models.BooleanField(default=False))

and in admin.py I've got the following (with fieldsets basically just copied from django/contrib/auth/admin.py)

class AdjUserAdmin(UserAdmin):
  list_display  = UserAdmin.list_display + ('new_field',)
  list_filter   = UserAdmin.list_filter + ('new_field',)

  fieldsets = UserAdmin.fieldsets
  fieldsets[1][1]['fields'] = ('first_name','last_name','email','new_field')

The problem is, when I do this I get the error:

AdjUserAdmin.fieldsets[4][1]['fields']' refers to field 'new_field' that is missing from the form.

I've looked at UserChangeForm, but it looks like it's already correctly pulling in User as the model. I'm not sure as to why new_field is missing from the form.

Thanks

In regards to this being smelly code

I know this is a smelly monkey patching way to go about doing this, but subclassing gives me issues mainly for these reasons.. if I could get it to work the way stated above, I'd be happy.. and maybe smelly.

In regards to the recommended way

I'm aware of the recommended way of creating a user profile, just that in particular situations, I don't see the merit in creating an entire new table and having an additional call to the database when all I want to store is an extra bit of information such as is_private or some such. If I'm storing lots more info, then I agree, setting up a user profile is preferable.

+2  A: 

First the "is it plugged in?" question -- Have you manually added new_field to the users table in the database? Syncdb wouldn't have taken care of that, of course.

After that, I would try appending the fields onto the existing UserAdmin rather than rebuilding it from scratch:

from django.contrib.auth.admin import UserAdmin
UserAdmin.list_display += ('new_field')
UserAdmin.list_filter += ('new_field')
UserAdmin.fieldsets += ('new_field',)
)
anschauung
Hmm.. dunno why I didnt think of that after using list_display = UserAdmin.list_display + ('new_field',).. thanks!
Silfheed
+3  A: 

It is preferred to write your own user profile class, and attach it to the User model. Then you can use the get_profile() method to retrieve the profile from the user.

Subclassing the profile admin from an Inline Admin should also allow you to edit the profile on the user's page, which is almost what you're trying to do.

This post has a really good write-up on the issue: http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/

sykora
A: 

I got the 'that is missing from the form' error, and discovered that it was due to my field being marked as 'editable=False' in the model.

stephendwolff