Hello,
I want to annotate a Queryset, order it by the annotated field and
then return a simple (ordered) values_list of the ids of the objects:
Wizard.objects.filter(**filterargs).annotate(wpower=Sum('creatures__power')).order_by('-
wpower').values_list('id', flat=True)
The 'Creature'-Model has a foreign key to 'Wizard' with
related_name='creatures', and it has a 'power'-IntegerField.
However, I get a 'FieldError: Cannot resolve keyword 'wpower' into
field
The only way that works is:
[
w.id for w in
Wizard.objects.filter(**filterargs).annotate(wpower=Sum('creatures__power')).order_by('-
wpower')]
or
[
w.id for w in
Wizard.objects.filter(**filterargs).annotate(wpower=Sum('creatures__power')).order_by('-
wpower').iterator()]
However, this takes more from the db than what I actually want. Any
ideas how I can achieve what I want with minimal db-load?