How to aggregate QuerySet based on property of model?
<p>For example some model:</p>

<pre><code>class Foo(models.Model):
    a = models.FloatField()
    b = models.FloatField()

    @property
    def c(self):
        return self.a / self.b
</code></pre>

<p>And we want to find minimal value in QuerySet:</p>

<pre><code>bar = Foo.objects.aggregate(Min('c'))
</code></pre>

<p>But this doesn't work, because <code>c</code> not in database and can't be fetched from db. How to get minimum value <code>c</code> of <code>Foo</code>?</p>

<p>You have to do the logic inside the query itself instead of as a property that is evaluated in Python.  Something like this should work:</p>

<pre><code>from django.db.models import F, Min

bar = Foo.objects.annotate(c=F('a') / F('b')).aggregate(Min('c'))
</code></pre>

python django django-queryset