views:

49

answers:

1

Heyas

I'd like to be able to search user profiles by username in the django admin. Essentially, in admin.py, I'd be doing something like:

class UserProfileAdmin(admin.ModelAdmin):
   search_fields = ['username']

but this wont work since user is a foreign key in the my usual user profile set up.

Is there a quick way to achieve this without having to add a username field to my UserProfile model?

Thanks

+4  A: 

This should do what you want:

class UserProfileAdmin(admin.ModelAdmin):
    search_fields = ['user__username']

assuming the name of your field that is foreignkey is user

Van Gale