10

I'm trying to get the Django Admin interface to display information about my profile. It displays all of my users but no profile information. I'm not quite sure how to get it to work.

I found this code after a quick google search:

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

admin.site.unregister(User)

class UserProfileInline(admin.StackedInline):
    model = UserProfile

class UserProfileAdmin(UserAdmin):
    inlines = [UserProfileInline]

admin.site.register(User, UserProfileAdmin)

However, I don't think that it worked. When I log into the admin page, I see Users, Groups, and Sites. I click Users and I see a list of all of my Users, but no indication of any profile. Clicking on a user shows me info about that user, but still no profile information.

If it will help, here is my model declaration:

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

class UserProfile(models.Model):
    company = models.CharField(max_length=30)
    user = models.ForeignKey(User, unique=True)

And my registration code:

def register(request):
    if request.method == 'POST':
        uf = UserForm(request.POST)
        upf = UserProfileForm(request.POST)
        if uf.is_valid() and upf.is_valid():
            user = uf.save()
            userprofile = upf.save(commit=False)#need to get the user profile object first
            userprofile.user = user #then set the user to user
            userprofile.save() #then save to the database
            return HttpResponseRedirect('/auth/login/')
    else:
        uf = UserForm()
        upf = UserProfileForm()
    return render_to_response('register.html', dict(userform=uf,userprofileform=upf),context_instance=RequestContext(request))
JPC
  • 8,096
  • 22
  • 77
  • 110

3 Answers3

26

I can't see exactly what's wrong, but here's a slightly simpler example that I know works. Put this is any working admin.py. Try adding a trailing comma to your inline-- some things break without it.

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

admin.site.unregister(User)

class UserProfileInline(admin.StackedInline):
    model = UserProfile

class UserProfileAdmin(UserAdmin):
    inlines = [ UserProfileInline, ]

admin.site.register(User, UserProfileAdmin)
Murat Çorlu
  • 8,207
  • 5
  • 53
  • 78
C. Alan Zoppa
  • 823
  • 8
  • 11
  • 1
    @JPC Which shouldn't matter, I know. Are you sure your admin.py is getting picked up? – C. Alan Zoppa Dec 30 '10 at 20:33
  • I think it worked. I can see the profile now when I click on a user! Wow thanks. A comma – JPC Dec 30 '10 at 20:34
  • Any idea how to add UserProfile information to the Users table so that I can sort by it/search by it? – JPC Dec 30 '10 at 20:40
  • 1
    Like sort in the admin app? It may be possible, but you'd probably have to do something inadvisable like subclass the User model. You're probably better off trying to monkey patch it in the template. – C. Alan Zoppa Dec 31 '10 at 00:52
  • Excellent answer. Thank you!!! I just miss you include User import from django.contrib.auth.models – Roldan Vargas Sep 26 '15 at 00:46
  • @C.AlanZoppa can you advise me with this? I need it to be visible on the django administration user profile, ONLY IF the user is in a specific group. – CDoc Dec 23 '19 at 11:12
6

This is not exactly an answer to your question BUT, according to Django Admin documentation, you can display information from UserProfile in your User "table". And you can make it searchable.

That would look something like this (modifying answer from C. Alan Zoppa):

class UserProfileAdmin(UserAdmin):
    inlines = [ UserProfileInline, ]
    def company(self, obj):
        try:
            return obj.get_profile().company
        except UserProfile.DoesNotExist:
            return ''
    list_display = UserAdmin.list_display + ('company',)
    search_fields = UserAdmin.search_fields + ('userprofile__company',)

You might have an issue though with search if your profile class is no longer called UserProfile.

Antoine Pelisse
  • 12,871
  • 4
  • 34
  • 34
3

The missing comma shouldn't matter. I suspect the problem is that you added a new admin.py but the development server didn't recognize it. If you restart the development server, it'll see the new file.

Joe Mornin
  • 8,766
  • 18
  • 57
  • 82