Thomas Kremmel
Thomas Kremmel

Reputation: 14783

django access related user model value in template

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. 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.

Upvotes: 2

Views: 1398

Answers (1)

ozan
ozan

Reputation: 9331

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?

Upvotes: 1

Related Questions