N
N
NyxDeveloper2021-05-20 19:21:57
Django
NyxDeveloper, 2021-05-20 19:21:57

How to annotate/aggregate with calculated fields?

For a long time I have been suffering from a lack of understanding of one simple thing - how to aggregate the property fields of a model?
Example:

class MyModel(models.Model):
    title = models.CharField('Название', max_length=100, unique=True)

    @property
    def test(self):
        return self.pk

How it is possible to execute aggregation with queryset on the test field? Show an example, please!
The usual doesn't work. Help me please! Thank you! UPD: If at all possible. queryset.annotate(lol_kek=Sum('test')).lol_kek

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max Rokitsky, 2021-08-14
@Exenzi

test is not a field in the database, but a method in the django model. On it it will not turn out to do aggregation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question