1

I have model UserProfile containing a OneToOneField on a class called Info:

class UserProfile(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    info = models.OneToOneField(Info, null = True, blank= True)

in my template I have access to the profile with

{{user.get_profile}}

but how to access Info? I have tried

{{user.get_profile.info.photo.url}}

without success.

lprsd
  • 84,407
  • 47
  • 135
  • 168
Mermoz
  • 14,898
  • 17
  • 60
  • 85

1 Answers1

6

You can't do that.

user.get_profile()

is a method and it gets executed when accessed as

{{user.get_profile}}

and not when you try to access more attributes of profile like {{user.get_profile.attr1}}

The way around is to send the profile object from the view. And if you want to have it in many view, you should make it a TemplateContextProcessor.

There is also another way of extending the User model with an attribute profile that is a @property that does the same as get_profile which you can do as I have described earlier.

Community
  • 1
  • 1
lprsd
  • 84,407
  • 47
  • 135
  • 168