简体   繁体   中英

Django MySql, foreign key query

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

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)

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM