I have model UserProfile
containing a OneToOneField
on a class called Info
开发者_如何学JAVA:
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.
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.
精彩评论