How to access foreign key data in template?

#1

Here is my query.

votes = Vote.objects.values('song__song_name', 'song__album__album_name', 'song__album__slug') \
                .annotate(vote_count=Count('song')) \
                .filter(vote_count__gt=1) \
                .order_by('-vote_count')[:6]

        return render(
        request,
        'home.html',
        context={
            'top6_votes': votes
            },
        )

How do I output song_name and album_name in the template? The obvious votes.song_name does not work.

{% for vote in top6_votes %}
        <div class="col-lg-4 py-2">
            <!-- Insert Album Cover -->
            <span class="fa-stack fa-3x">
            <!-- The icon that will wrap the number -->
            <span class="fa fa-certificate fa-stack-2x poison-purple-text"></span>
            <!-- a strong element with the custom content, in this case a number -->
            <strong class="fa-stack-1x poison-green">
                {{ forloop.counter }}
            </strong>
        </span>
            <h4 class="mt-2">
                {{ vote.song_set.song_name }}
            </h4>
            <p>{{ vote.vote_count }}</p>
        </div>
    <!-- end loop -->
    {% endfor %}

Thanks!

0 Likes

(Basil Jose) #2
{% for vote in top6_votes %}
        <div class="col-lg-4 py-2">
            <!-- Insert Album Cover -->
            <span class="fa-stack fa-3x">
            <!-- The icon that will wrap the number -->
            <span class="fa fa-certificate fa-stack-2x poison-purple-text"></span>
            <!-- a strong element with the custom content, in this case a number -->
            <strong class="fa-stack-1x poison-green">
                {{ forloop.counter }}
            </strong>
        </span>
            <h4 class="mt-2">
                {{ vote.song__song_name }}
            </h4>
            <p>{{ vote.vote_count }}</p>
        </div>
    <!-- end loop -->
    {% endfor %}

for album name you must use like {{vote.song__album__album_name}}

0 Likes

#3

Thanks!
Had to change {{ vote.song_set.song__song_name }} to {{ vote.song__song_name }} so it’s all good!

0 Likes

#4

Can someone please explain to me what the reason is for not being able to do song_list.album__album_name with this queryset, and what I have to do to get it? It’s practically the same as above, but it doesn’t work the same way. Frustrating. :woozy_face:

I have tried every combination I can think of.

<QuerySet 
[
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': 'Little Willy', 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 1, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': 'Suffragette City', 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 2, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': 'I Never Cry', 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 3, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': 'I Need to Know', 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 4, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': "Can't You See", 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 5, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': 'What I Like About You', 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 6, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': 'Dead Flowers', 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 7, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': 'Just What I Needed', 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 8, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': 'Rock and Roll All Nite', 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 9, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': "You Don't Mess Around with Jim", 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 11, 'with_cc': True},
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': "We're an American Band", 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 13, 'with_cc': True}, 
{'album__album_name': "Poison'd!", 'album__slug': 'poisond', 'song_name': 'SexyBack', 'album__year_released': 2007, 'album__month_released': 3, 'track_no': 14, 'with_cc': True}
]>
0 Likes

#5

OK, I just figured it out. I didn’t want to loop over the whole set just to get album name, only wanted to do it for songs. So I needed to retrieve only the first row by using first
{{ song_list.first.album__album_name }}

It’s differs from the other one I posted in that I was looping over all albums AND songs.

But, it causes another SQL query, so if there is a better way, would love to know.

0 Likes