GistDom Blog
GistDom Blog

Reputation: 51

How can I get item from a model field

I'm a Django beginner, how can I get profile_pic from Profile Model connecting it to 'to_user' field in FriendRequest Model.

I was able to get the names of users in 'to_user' field by this:

{% for data in sent_friend_request %}
{{ data.to_user.username }}
{% endfor %}

How do I get the profile_pic for each users?

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    profile_pic = models.ImageField()

class FriendRequest(models.Model):
    to_user = models.ForeignKey(settings.AUTH_USER_MODEL)
    from_user = models.ForeignKey(settings.AUTH_USER_MODEL)

def following_view(request, username):
     sent_friend_request = FriendRequest.objects.filter(from_user__username=username)

context = {'sent_friend_request':sent_friend_request}

{% if data.to_user.profile_pic %} 
<img src="{{ data.to_user.profile_pic }}">
{% endif %} 

Upvotes: 0

Views: 40

Answers (1)

Arjun Shahi
Arjun Shahi

Reputation: 7330

You can do like this:

{% if data.to_user.profile.profile_pic %} 

<img src="{{ data.to_user.profile.profile_pic.url }}">

{% endif %}

Upvotes: 1

Related Questions