Django update queryset with annotation
For Django 1.11+ you can use Subquery: from django.db.models import OuterRef, Subquery, Sum Relation.objects.update( rating=Subquery( Relation.objects.filter( id=OuterRef(‘id’) ).annotate( total_rating=Sum(‘sign_relations__rating’) ).values(‘total_rating’)[:1] ) ) This code produce the same SQL code proposed by Tomasz Jakub Rup but with no use of RawSQL expression. The Django documentation warns against the use of RawSQL due to the possibility of … Read more