Child/parent hierarchy queries

17 views
Skip to first unread message

Richard Jackson

unread,
Nov 21, 2020, 3:46:36 PM11/21/20
to Mezzanine Users
Hi there!

I have four models: "Encyclopedia", "Song", "Section", "Chord". In the admin panel I have set this up with Encyclopedia being parent to Song being parent to Section being parent to Chord (i.e. there's an Encyclopedia which has a bunch of Songs, each Song is made of Sections, each Section has a bunch of Chords). The admin looks like this:

Song-Chord-Hierarchy.png

1) How can I query the Song dataset to access all its child Chords? (i.e. for each Song, return a list of all Chords)

My current method is to make datasets for Songs, Sections and Chords, pass these datasets to the html file, then script the html as so:

{% for song in songs %}
    {% for section in sections %}
        {% if section.parent.title == song.title %}
            {% for chord in chords %}
                {% if chord.parent.title == section.title and chord.parent.parent.title == section.parent.title %}
                    [ do stuff ]

I'd greatly appreciate advice on better practices for this!

2) How can I query the Chord dataset to access what Song is the parent? Chord.parent gives me the full hierarchy, but I can't work out if there's a way to specifically jump up two levels (in this instance Chord is child to Section, and Section is child to Song, and Song is what I'm looking to identify).

Thanks!

Rich


Richard Jackson

unread,
Nov 22, 2020, 7:03:07 AM11/22/20
to mezzani...@googlegroups.com
2) Parent two levels up can be accessed as:

for chord in chords:
    chord.parent.parent.title

--
You received this message because you are subscribed to a topic in the Google Groups "Mezzanine Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mezzanine-users/44jYmDLnRFY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mezzanine-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mezzanine-users/0cffd480-dc2f-43df-ad48-c2110bb5b96cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages