0

I have the following Models in Django.

from django.db import models

#Show DB Table Model

class Shows(models.Model):
    show_key = models.CharField(primary_key=True, max_length=7)
    show_date = models.DateField(blank=True, null=True)
    show_venue = models.CharField(max_length=50, blank=True, null=True)
    show_city = models.CharField(max_length=50, blank=True, null=True)
    show_state = models.CharField(max_length=3, blank=True, null=True)
    show_country = models.CharField(max_length=3, blank=True, null=True)

class Meta:
    managed = False
    db_table = 'shows'

#Songs DB Table Model

class Songs(models.Model):
    song_key = models.CharField(primary_key=True, max_length=8)
    show_key = models.ForeignKey('Shows', models.DO_NOTHING, db_column='show_key', blank=True, null=True)
    song_name = models.CharField(max_length=100, blank=True, null=True)
    song_set = models.CharField(max_length=20, blank=True, null=True)
    song_track = models.IntegerField(blank=True, null=True)
    song_encore = models.IntegerField(blank=True, null=True)
    song_segue = models.CharField(max_length=1, blank=True, null=True)
    song_notes = models.CharField(max_length=100, blank=True, null=True)
    song_cover = models.CharField(max_length=50, blank=True, null=True)
    song_with_guest = models.CharField(max_length=50, blank=True, null=True)



class Meta:
    managed = False
    db_table = 'songs'

I am trying make a query that will find all objects meeting a certain criteria, ie:

Shows.objects.filter(show_date__year=2000)

This above query would return multiple objects.

I need to take it a step further and pull all of the information from the Songs table/model relating to the filtered Show objects. The models are related in the sense that the "show_key" is a primary key / foreign key relationship and is one to many.

I also need to package all of the found data up into a usable form that I can iterate through and send to a jinja2 template.

For example:

{% for item in query_results %}
<ul>
    <li>item.show_date</li>
    <li>item.show_venue</li>
    <li>item.show_city</li>
    <li>item.show_state</li>
    <li>item.show_country</li>
</ul>
    <ul>
    {% for song in item %}
        <li>song.song_name</li>
        <li>song.song_set</li>
        <li>song.song_track</li>
        <li>song.song_encore</li>
        <li>song.song_segue</li>
        <li>song.song_notes</li>
    </ul>
    {% endfor %}

Thanks in advance. Brent

Brent Vaalburg
  • 119
  • 2
  • 11
  • Possible duplicate of [Django Reverse Query in Template](https://stackoverflow.com/questions/6306568/django-reverse-query-in-template) – Eduardo Apr 13 '18 at 23:20

2 Answers2

0

Seems like what you're trying to do is follow a FK relationship backwards.

This is what it should look like in the template:

{% for show in query_results %}
<ul>
    <li>show.show_date</li>
    <li>show.show_venue</li>
    <li>show.show_city</li>
    <li>show.show_state</li>
    <li>show.show_country</li>
</ul>
    <ul>
    {% for song in show.entry_set.all %}
        <li>song.song_name</li>
        <li>song.song_set</li>
        <li>song.song_track</li>
        <li>song.song_encore</li>
        <li>song.song_segue</li>
        <li>song.song_notes</li>
    </ul>
   {% endfor %}

This will actually force jango to issue one SQL query for every show. If you have too many it can be a pain. To avoid this you can tell Django to select related songs data when it queries for the shows. This can save you a lot of SQL queries.

Shows.objects.select_related(Songs).filter(show_date__year=2000)
Eduardo
  • 22,574
  • 11
  • 76
  • 94
  • Thank you very much for your response. Unfortunately I am still having issues. Even running just Shows.objects.select_related(Songs) gives me an AttributeError: type object 'Songs' has no attribute 'split' , what am I missing here? – Brent Vaalburg Apr 14 '18 at 01:06
  • I assume this is because select_related expects the Object Songs to be a string. How can I select_related Objects? – Brent Vaalburg Apr 14 '18 at 01:09
0

I finally figured it out!

First I queried the Shows model/table and saved the results to query_results:

query_results = Shows.objects.filter(show_date__year=2018)

Then in my Jinja Template

{% for show in query_results %}
<ul>
    <li>{{show.show_date}}</li>
    <li>{{show.show_venue}}</li>
    <li>{{show.show_city}}</li>
    <li>{{show.show_state}}</li>
    <li>{{show.show_country}}</li>
</ul>
    <ul>
    {% for song in show.songs_set.all %}  #This is the key, calling the related "shows_set.all" This grabs the related objects from the Songs table/model
        <li>{{song.song_name}}</li>
        <li>{{song.song_set}}</li>
        <li>{{song.song_track}}</li>
        <li>{{song.song_encore}}</li>
        <li>{{song.song_segue}}</li>
        <li>{{song.song_notes}}</li>
    {% endfor %}
    </ul>
{% endfor %}
Brent Vaalburg
  • 119
  • 2
  • 11