3

I'm new with Django and I'm having some problems creating a custom user model. I followed every steps from the django documentation. Here is my model :

    class UserProfile(models.Model):
user = models.OneToOneField(User)
comment = models.BooleanField()
score = models.IntegerField(null=True)
profilpic = models.ImageField(upload_to="/profilepics")
bio = models.CharField(max_length=140)

Then I created several users with django-registration. But when I go to the admin and I try to delete a user I created or when I just try to click on the username, I get this error:

AttributeError at /admin/auth/user/3/
'UserProfile' object has no attribute 'username'
Exception Value:    
'UserProfile' object has no attribute 'username'
Exception Location: /Users/marc-antoinelacroix/Desktop/Site/sportdub/projet/models.py in   __unicode__, line 14

So I think I have to create a "username" in my UserProfile model, and associate it to the username of the django's User, but I have no idea how to do it...

Any help would be welcome.

Thanks!

Marcolac
  • 901
  • 4
  • 14
  • 27

2 Answers2

5

It seems like you're trying to access

def __unicode__(self):
    return self.username

but it has to be

def __unicode__(self):
    return self.user

Here's a demo

project/account/models.py

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    homepage = models.URLField(verify_exists=False)
    #...

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

project/account/admin.py

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

admin.site.unregister(User)

class UserProfileInline(admin.StackedInline):
    model = UserProfile

class UserProfileAdmin(UserAdmin):
    inlines = [UserProfileInline]

admin.site.register(User, UserProfileAdmin)

project/settings.py

AUTH_PROFILE_MODULE = "account.userprofile"
Thomas Schwärzl
  • 9,518
  • 6
  • 43
  • 69
  • Hi. I tried your solution but it doesn't seem to work, I still get the exact same error. It comes from __unicode__ apparently. But thanks! – Marcolac Oct 11 '12 at 10:10
  • what is in line14 of this file? `sportdub/projet/models.py in __unicode__, line 14` – Thomas Schwärzl Oct 11 '12 at 10:32
  • and 15? ...or could you post the whole `models.py` file from your `projet` folder? – Thomas Schwärzl Oct 11 '12 at 11:22
  • 1
    thank you, I did try to access return self.username. I replaced it bu return self.user, but know I get the error: TypeError at /admin/auth/user/3/ coercing to Unicode: need string or buffer, User found – Marcolac Oct 11 '12 at 11:45
  • Ok, and I found the solution for this error here: http://stackoverflow.com/questions/7399283/django-auth-user-in-admininterface-coercing-to-unicode-need-string-or-buffer – Marcolac Oct 11 '12 at 11:47
  • changed `return self.username` to `return self.user.username`. Now it works like a charm. – username Nov 15 '12 at 13:34
0

No, you need to define UserProfile.__unicode__() properly. It needs to get the username from the related User model.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Ok, what I currently have is: def __unicode__(self): return self.username How do I relate it to the username? Do I have to use "username" as an argument for __unicode()__? – Marcolac Oct 11 '12 at 10:11