2

i have this custom class

class CustomUserAdmin(UserAdmin):
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'email', 'password1', 'password2', 'location')}
        ),
    )
    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',)}),
    )
    #UserAdmin.list_display += ('location',)
    add_form = MyUserCreationForm
    form = MyUserChangeForm

It works fine, untill i uncomment this line

UserAdmin.list_display += ('location',)

then it gives me this error: CustomUserAdmin.list_display[5], 'location' is not a callable or an attribute of 'CustomUserAdmin' or found in the model 'User'.

any help?

[Edit]

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    location = models.CharField(max_length=30)

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

1 Answer 1

2

You are not going to modify UserAdmin right?

Supposing that location is an actual field of CustomUser, try to use

list_display = UserAdmin.list_display + ('location',)

EDIT: simpler answer

Use the standard django way to show custom things in the list_display:

class CustomUserAdmin(UserAdmin):
    # other things

    def user_location(self, u):
        try:
            return u.get_profile().location
        except:
            return ''
    user_location.short_description = 'Location'

    list_display = UserAdmin.list_display + ('user_location',)

EDIT: more info

Anyway, if you are extending the UserForm just for the sake of adding the profile fields, you should look into this link: http://www.thenestedfloat.com/articles/displaying-custom-user-profile-fields-in-djangos-admin/index.html to take advantages of inlines and avoid recreating the whole Form from scratch.

Sign up to request clarification or add additional context in comments.

4 Comments

that didnt seem to help Im still getting the same error. the location field worked for add_fieldsets and fieldsets, so assume it should work for list_display. i also tried doing this: list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'location')' it gave me the same error
Could you post the CustomUser model?
This question was very helpful (I was using inlines but it still applied) but it wasn't working for me because I forgot to add the following to settings: AUTH_PROFILE_MODULE = 'myapp.UserProfile'
The link is broken, here it is from archive.org: web.archive.org/web/20170424191936/http://…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.