I,m trying to annotate a sum of another model, but filtered by date.
I have the models Employee
and Shift
, the Shift
one has a DecimalField called dur
, a DateTimeField start
and a foreign key employee.
class Employee(models.Model):
name = models.CharField(max_length=64)
class Shift(models.Model):
employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
start = models.DateTimeField()
dur = models.DecimalField(max_digits=4, decimal_places=2, default=0)
With Employee.objects.all().annotate(Sum('shift__dur'))
I could add the total of all shifts, but how can I filter these shifts, e.g. to sum just the shifts in a given date range?
Thanks in advance!