[Answered ]-Django annotate with complex value

2👍

No, you can’t pass math functions into annotate().

If you want to do this calculation in Test model then create a method:

class Test(models.model):
    value = models.PositiveIntegerField(_('value'))

    def calc_value(self, next):
        return math.sqrt(next-self.value)/math.atan(self.value))

for t in Test.objects.all():
    print t.value. t.calc_value(5)

But if you want to use this calculation to order the queryset then you have to do the math at SQL level:

next = 5
Test.objects.extra(select={'new_field': 'SQRT(%d-value)/ATAN(value)' % next}) \
            .order_by('new_field'))

To filter the queryset by new field use where argument of the same extra() method:

Test.objects.extra(select={'new_field': 'SQRT(%d-value)/ATAN(value)' % next},
                   where=['new_field > 10'])

SQLite doesn’t support math functions by default but with Postgres and MySQL this code should work just fine.

0👍

No, annotations can only be done on django aggregations.

Annotates each object in the QuerySet with the provided list of aggregate values (averages, sums, etc) that have been computed over the objects that are related to the objects in the QuerySet.

Leave a comment