How do I make tables using one of models field as headers

Hello!

I got a question how do I make tables using one of models field as headers, let me explain:

My models (please, don’t mind native language):

class Timeperiod(models.Model):
    '''Периоды.'''
    start_date = models.DateField('дата начала')
    end_date = models.DateField('дата окончания')
    target_duration = models.DurationField('длительность')
    is_open = models.BooleanField('открыт', default=False)

    class Meta:
        verbose_name = 'период'
        verbose_name_plural = 'периоды'

    @property
    def test(self) -> str:
        return 'test'

    @property
    def days(self) -> list:
        delta = self.end_date - self.start_date
        ds = []
        for i in range(delta.days + 1):
            ds.append(self.start_date + timedelta(days=i))
        return ds

    def __str__(self) -> str:
        return ' - '.join([str(self.start_date), str(self.end_date), str(self.target_duration), str(self.days)])
class Timetracking(models.Model):
    '''Списания времени.'''
    employee = models.ForeignKey(Employee, on_delete=models.PROTECT, verbose_name='сотрудник')
    project = models.ForeignKey(Project, on_delete=models.PROTECT, verbose_name='проект')
    timeperiod = models.ForeignKey(Timeperiod, on_delete=models.PROTECT, verbose_name='период')
    date = models.DateField('дата')
    time = models.DurationField('трудозатраты')

    class Meta:
        verbose_name = 'списание'
        verbose_name_plural = 'списания'

    def __str__(self) -> str:
        return f'{self.employee} - {self.project}: {self.date} {self.time}'

I want to have data displayed like this:
Timeperiod.days[1] | … Timeperiod.days[n]
sum(all_timetrackings_at_that_day) | sum(all_timetrackings_at_that_day)

But I just have now idea where to start to get data like this

Are you talking about creating an html table this way?

If so, you can iterate over that list using the template for tag.

something like this as a starting point (assuming i’ve understood the models … :p)?

time_periods = TimePeriod.objects.filter(...).annotate(
    timetracking_time_sum=Subquery(
        Timetracking.objects.filter(
            timeperiod__pk=OuterRef('pk'),
        ).values('timeperiod__pk').order_by('timeperiod__pk').annotate(
            total=Sum('time')
        ).values_list(
            'total'
        )[:1]
        output_field=models.DurationField(),
        default=Value(0)
    )
)

and then as Ken says, simple loop.

Thank you gentlemen, that was exactly what I needed!