Please note: Similar questions didn't help me as they have the category-foreignkey in the same class.

I have a simple Invoice app with models Invoice, Position, Product and Category. The Product is bound to the Category.
My target is to create a queryset that

* filters e. g. a specific date-range
* and then group all categories and build their sums

Here is a screenshot of the invoice respectively of its positions:
[![enter image description here](https://i.stack.imgur.com/gJzI1.png)](https://i.stack.imgur.com/gJzI1.png)

The expected result of the grouped query should look like this:

[![enter image description here](https://i.stack.imgur.com/trjvi.png)](https://i.stack.imgur.com/trjvi.png)

Can you help me to create a query that groups and sums the categories within the filtered date-range?

The only solution I was able to create was the filter of a specific date-range:

```
queryset = Position.objects.filter(invoice__date_of_purchase__range=['2019-01-01', '2019-12-31']) 

```

models.py (which I have simplified):

```
from django.db import models
from django.urls import reverse

class Category(models.Model):
    name = models.CharField(max_length=30, unique=True)

    def __str__(self):
        return self.name

class Product(models.Model):
    name = models.CharField(max_length=120)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='products')

    def __str__(self):
        return self.name

class Invoice(models.Model):
    invoice_code = models.CharField(max_length=15)
    date_of_purchase = models.DateField()
    customer_name = models.CharField(max_length=100)

    def __str__(self):
        return self.invoice_code

class Position(models.Model):
    invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.DecimalField(decimal_places=2, max_digits=6)
    price = models.DecimalField(decimal_places=2, max_digits=8)
    total = models.DecimalField(
        decimal_places=2, max_digits=8, blank=True, null=True)  # is calculated in view

    def __str__(self):
        return self.product.name

```