1

I am trying to extend the default User Model in django.

I have defined a class called Profile.

# code in myapp/models/profile.py

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

    class Profile(models.Model):
        user = models.OneToOneField(User)
        field = models.CharField(max_length=30)
        class Meta:
            app_label="myapp"

In my view function I invoke a method where I get an exception:

def my_view_function(request):
    field = get_field(request.user)
    ...

def get_field(user):
   return user.profile.field
   # also tried return user.get_profile().field with AUTH_PROFILE_MODULE in settings.py set to "myapp.Profile", still does not work.

I get an AttributeError in my stacktrace 'unicode' object has no attribute 'get_profile' or 'unicode' object has no attribute 'profile'.

When I try to concatenate the user object in get_field() with a string I get an error saying 'str' cannot concatenate with 'SimpleLazyObject'. I tried reading request.user returns a SimpleLazyObject, how do I "wake" it? but that did not help.

I am using middleware to ensure that the user is logged in so I am sure that the user is logged in. What is wrong with the user object? Why does it django say user object has '__unicode__' type.

Thanks in Advance!

Community
  • 1
  • 1
Rohit Banga
  • 18,458
  • 31
  • 113
  • 191
  • Can you show us the code you're using when you actually call the `get_field` function? You're probably passing it something incorrectly. Also, what version of Django are you using? (You can tell if you don't know using `pip freeze`.) – jdotjdot Dec 30 '12 at 17:40
  • I use precisely get_field(request.user) when calling get_field function. The name of the function is different but that should not matter. – Rohit Banga Dec 30 '12 at 17:45

2 Answers2

3

Assuming that you have AUTH_PROFILE_MODULE = 'yourapp.Profile' in your settings.py you should be able to call the get_profile() method on the User object to get the Profile model (for example, request.user.get_profile()).

To get the actual field, you would need to call request.user.get_profile().field.

You should note that user profiles are deprecated as of django 1.5 in favor of custom user models.

dgel
  • 16,352
  • 8
  • 58
  • 75
  • its not working. I assume request.user.get_profile() is equivalent to passing request.user to a function and calling user.get_profile() there. Note Profile class is present in myapp/models/profile.py. But I still set AUTH_PROFILE_MODULE to "myapp.Profile". – Rohit Banga Dec 30 '12 at 17:44
  • Well, what is happening when you try? – dgel Dec 30 '12 at 17:45
  • I am guessing that your middleware is somehow changing `request.user` from a `User` instance to a string (unicode). – dgel Dec 30 '12 at 17:48
  • Using this middleware: http://onecreativeblog.com/post/59051248/django-login-required-middleware – Rohit Banga Dec 30 '12 at 17:49
  • Hmm.. That middleware doesn't appear to replacing `request.user`. You should add a `print type(request.user)` at the top of your view function for debugging. That will output to the console the type of the instance. If that isn't a `User` instance, something, somewhere is replacing it. – dgel Dec 30 '12 at 17:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21904/discussion-between-iamrohitbanga-and-dgel) – Rohit Banga Dec 30 '12 at 17:59
  • Glad to hear it. Sorry I couldn't chat- got pulled away. – dgel Dec 30 '12 at 19:54
0

Fixed it. I looked at the stack trace carefully and noticed I was passing the field value to another function which expected a user object. This led to the confusing __unicode__ error.

Rohit Banga
  • 18,458
  • 31
  • 113
  • 191