Можно ли использовать условие if в шаблоне или html для устранения дублирующихся данных?
Примечание: у меня уже есть отдельные ядра в моих представлениях, но так как у меня есть 2 цикла в 1 дублирует некоторые данные.
это мои html
{% for core in cores %}
{% for behavior in behaviors %}
{% if core.Grading_Behavior__Grading_Behavior__Name == behavior.Grading_Behavior__Grading_Behavior__Name %}
<tr>
<td rowspan="2" colspan="4" class="tblcoretitle">{{core.Grading_Behavior__Grading_Behavior__Name}} 1</td>
{% if core.Grading_Behavior__Grading_Behavior__GroupName == behavior.Grading_Behavior__Grading_Behavior__GroupName %}
<td colspan="4" class="tblcore"> {{behavior.Grading_Behavior__Grading_Behavior__GroupName}}
</td>
{% else %}
{% endif %}
<td class="tblcore">1</td>
<td class="tblcore">2</td>
<td class="tblcore">3</td>
<td class="tblcore">4</td>
</tr>
<tr>
{% if core.Grading_Behavior__Grading_Behavior__GroupName == behavior.Grading_Behavior__Grading_Behavior__GroupName %}
{% else %}
<td colspan="4" class="tblcore">{{behavior.Grading_Behavior__Grading_Behavior__GroupName}}
</td>
{% endif %}
</tr>
{% endif %}
{% endfor %}
{% endfor %}
это мои views.py
cores = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
.filter(Students_Enrollment_Records__in=Students.values_list('id')).values('Grading_Behavior__Grading_Behavior__Name','Grading_Behavior__Grading_Behavior__GroupName').distinct('Grading_Behavior__Grading_Behavior__Name')\
.order_by('Grading_Behavior__Grading_Behavior__Name')
behaviors = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
.filter(Students_Enrollment_Records__in=Students.values_list('id')).filter().values('Grading_Behavior__Grading_Behavior__Name','Grading_Behavior__Grading_Behavior__GroupName').distinct('Grading_Behavior__Grading_Behavior__GroupName')\
.order_by('Grading_Behavior__Grading_Behavior__GroupName')
это мой текущий результат

это мне нужен результат

приятель, если у вас есть лучшее решение или идея, поделитесь ответом.
ОБНОВЛЕНИЕ
когда я попробовал это в представлениях,
cores = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
.filter(Students_Enrollment_Records__in=Students.values_list('id')).values(
'Grading_Behavior__Grading_Behavior__Name', 'Grading_Behavior__Grading_Behavior__GroupName').distinct(
'Grading_Behavior__Grading_Behavior__Name') \
.order_by('Grading_Behavior__Grading_Behavior__Name')
behaviors = StudentsBehaviorGrades.objects.filter(Teacher=teacher) \
.filter(Students_Enrollment_Records__in=Students.values_list('id')).values(
'Grading_Behavior__Grading_Behavior__Name', 'Grading_Behavior__Grading_Behavior__GroupName').distinct(
'Grading_Behavior__Grading_Behavior__GroupName') \
.order_by('Grading_Behavior__Grading_Behavior__GroupName')
matches = cores.union(behaviors)
мой html
{% for match in matches %}
<tr>
<td rowspan="2" colspan="4" class="tblcoretitle">{{match.Grading_Behavior__Grading_Behavior__Name}} 1</td>
</tr>
<tr>
<td colspan="4" class="tblcore">{{match.Grading_Behavior__Grading_Behavior__GroupName}}</td>
<td class="tblcore">1</td>
<td class="tblcore">2</td>
<td class="tblcore">3</td>
<td class="tblcore">4</td>
</tr>
{% endfor %}
это результат:

он дублирует основные значения
Собирались решить эту пару, немного больше регулировки. заранее спасибо.
ОБНОВЛЕНИЕ еще раз
когда я использую
matches = itertools.chain(cores, behaviors)
результат
