0

I am trying to query a queryset in Django using annotate to get a calculation for each different 'field2'. The query looks like:

offer_lines.filter('field1'=x).order_by().values('field2').annotate(
                total=F('field3') * F('field4')

While I am expecting it to return something that looks like this:

[{'field2_value1': '1'}, {'field2_value2': '2'}]

I am getting multiple entries for the same field2 value. So it looks more like this:

[{'field2_value1': '1'}, {'field2_value1': '2'}, {'field2_value1': '5'},{'field2_value2': '2'}, {'field2_value2': '1'}]

I have order_by() in the query as I saw in other questions that it sometimes matters, but it did not help me. How can I query it to only return one entry per each unique field2 value?

Sherpa
  • 1,948
  • 13
  • 25
clue3434
  • 215
  • 1
  • 2
  • 8

1 Answers1

0

You can try chaining the distinct method

docs - here

iamkhush
  • 2,562
  • 3
  • 20
  • 34