开发者

django access related user model value in template

开发者 https://www.devze.com 2022-12-17 19:14 出处:网络
I have a model looking like this. class ProjectMembership(models.Model): member = models.ForeignKey(User, related_name=\'project_membership_member_set\')

I have a model looking like this.

class ProjectMembership(models.Model):
    member = models.ForeignKey(User, related_name='project_membership_member_set')

Edit: In a template I want now to access the last_name of the User model. I thought it should work like the following line, but it does not.

{{ project_membership.member.last_name }}

No error is provided.开发者_JS百科 Just the value is missing. I just want to print out the first and last name of the User object referenced in the variable member. Interestingly

{{ project_membership.member }}

does work. This prints out the “human-readable” representation of the User object.


If project_membership.member gives you the user, then project_membership.member.last_name should give you that user's last_name.

Are you absolutely sure that the user you're testing for has last_name set? Do you get any output on project_membership.member.username? If you try to access project_membership.member.last_name in your view or through the shell, do you get an error or an empty unicode string?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号