Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - User, UserProfile, and Admin

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))
like image 752
JPC Avatar asked Dec 30 '10 19:12

JPC


People also ask

What is the username and password for Django admin?

Username: ola Email address: [email protected] Password: Password (again): Superuser created successfully. Return to your browser. Log in with the superuser's credentials you chose; you should see the Django admin dashboard.

Can Django handle multiple users?

1. No matter what strategy you pick, or what is your business model, always use one, and only one Django model to handle the authentication. You can still have multiple user types, but generally speaking it's a bad idea to store authentication information across multiple models/tables.

How do I restrict access to admin pages in Django?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .


1 Answers

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)
like image 159
C. Alan Zoppa Avatar answered Oct 28 '22 14:10

C. Alan Zoppa