Group and get unique values from attribute

Hi everyone! Hope you’re doing well I’m doing a translator database, where a translator can login, go to their profile and add a movie, tv show, etc. that they translated, the episodes or season they translated and with they “role” (subtitling authoring, just translation, etc.) Here are my models:

class Work(models.Model):
    TYPES = [
        ("F", "Movie"),
        ("S", "TV Series"),
        ("C", "Short"),
        ("D", "Documentary"),
        ("R", "Reality Show"),
        ("P", "TV Show")
    ]

    type = models.CharField(choices=TYPES, max_length=255)
    tmdb = models.IntegerField()
    title = models.CharField(max_length=255)
    year = models.IntegerField()
    slug = models.SlugField()

    def __str__(self):
        return str(self.title)

class TranslatedWork(models.Model):
    work = models.ForeignKey(Work, on_delete=models.CASCADE, default="")
    member = models.ForeignKey(Member, on_delete=models.CASCADE, default="")
    platform = models.CharField(max_length=255)
    season = models.IntegerField(blank=True, null=True)
    episode = models.CharField(blank=True, null=True, max_length=255)

    def __str__(self):
        return str(self.work)

class Subtitling(TranslatedWork):
    translation = models.ForeignKey(Member, on_delete=models.CASCADE, default="", related_name="translation")
    adaptation = models.ForeignKey(Member, on_delete=models.CASCADE, default="", related_name="adaptation")
    sub = models.ForeignKey(Member, on_delete=models.CASCADE, default="", related_name="subtitling")

class Dubbing(TranslatedWork):
    translation = models.ForeignKey(Member, on_delete=models.CASCADE, default="", related_name="traducao_vp")
    adaptation = models.ForeignKey(Member, on_delete=models.CASCADE, default="", related_name="adaptacao_vp")
    timing = models.ForeignKey(member, on_delete=models.CASCADE, default="", related_name="temporizacao_vp")
    proofreading = models.ForeignKey(Member, on_delete=models.CASCADE, default="", related_name="revisao_vp")

class Accessibility(TranslatedWork):
    audio_description = models.ForeignKey(Member, on_delete=models.CASCADE, default="", related_name="guiao_ad")
    sdh = models.ForeignKey(Member, on_delete=models.CASCADE, default="", related_name="lse")

Admin:

class SubtitlingInline(admin.TabularInline):
    model = Subtitling
    extra = 0

class DubbingInline(admin.TabularInline):
    model = Dubbing
    extra = 0

class AccessibilityInline(admin.TabularInline):
    model = Accessibility
    extra = 0

class WorkAdmin(admin.ModelAdmin):
    inlines = [SubtitlingInline, DubbingInline, AccessibilityInline]

admin.site.register(Member)
admin.site.register(Work, WorkAdmin)```

Basically, I want the /details/work-slug page to have four bootstrap tabs (Info, Subtitling, Dubbing, Accessibility) and, inside of the last three, if the work.type == "TV Series", an accordeon for each season. And inside each accordeon, the episodes and the respective translators.

Here's what I've tried to do:

      {% if work.type == "S" or work.type == "R" or work.type == "P" %}
        {% for sub in subtitling %}
          {% if sub.season %}
        <div role="tablist" class="accordion" id="temporada{{ sub.season }}">
            <div class="accordion-item">
                <h2 role="tab" class="accordion-header"><button data-bs-toggle="collapse" data-bs-target="#temporada{{ sub.season }} .item-1" aria-expanded="true" aria-controls="temporada{{ sub.season }} .item-1" class="accordion-button">Season {{ sub.season }}</button></h2>
                <div role="tabpanel" data-bs-parent="#temporada{{ sub.season }}" class="accordion-collapse collapse show item-1">
                    <div class="accordion-body">
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                      <th>Episode</th>
                                      {% if sub.translation %} <th>Translation</th> {% endif %}
                                      {% if sub.adaptation %} <th>Adaptation</th> {% endif %}
                                      {% if sub.sub %} <th>Subtitling</th> {% endif %}
                                      <th>Platform</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>{{sub.episode}}</td>
                                        {% if sub.translation %} <td>{{sub.translation}}</td> {% endif %}
                                        {% if sub.adaptation %} <td>{{sub.adaptation}}</td> {% endif %}
                                        {% if sub.sub %} <td>{{sub.sub}}</td> {% endif %}
                                        <td>{{sub.platform}}</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
          {% endif %}
        {% endfor %}
      {% endif %}```

Here’s the result. The season 1 accordeons should be merged:

In case it helps, here’s my inspiration:

Solved:
I had to order by season in the views before regrouping