Calculate Duration with a Manager, restricting aggregation to entries...

Hi again folks,

I am trying to calculate a sum of work-years based on database entries. In the end, for each employee, there should be displayed his total work-time in the department based on one or more employments.

basically, I tried it with THIS:

class WorkYearsManager(models.Manager):

    def with_work_years(self):
        today =
        return self.annotate(
                (F('duty_end') - F('duty_begin')), output_field=fields.FloatField()
                (today - F('duty_begin')), output_field=fields.FloatField()

    def total_work_years(self, employee):
        return self.with_work_years().filter(employee=employee).aggregate(
            total_work_years=Sum('past_years') + Sum('ongoing_years')

but of course, here will be calculatet the duty_end - duty_begin duration for EACH employment AND the today - duty_begin duration also for EACH employment…which leads to a way too large sum.

So, i need to restrict the ongoing_years calculation on the actual employment and the past_years calculation to all former employments…