Example model:
class A(models.Model):
id = models.CharField(max_length=255, primary_key=True, default=make_uuid, editable=False)
b = models.IntegerField()
Goal:
I need to get list which would contain id, b and same_b_total.
e.g. following quetyset returns:
a = list(models.Cell.objects.all().values("b").annotate(same_b_total=Count("b")))
print(a) # [{"b": 1, "same_b_total": 5}, {"b": 2, "same_b_total": 3}]
When I add id into .values("b", "id") it return list with followind data
[{'b': 1, 'id': '<some uuid>', 'same_b_total': 1}, {'b': 1, 'id': '<some uuid2>', 'same_b_total': 1}, {'b': 2, 'id': '<some uuid3>', 'same_b_total': 1}, ...]
How to change query to receive correct same_b_total for each record? Like:
[{'b': 1, 'id': '<some uuid>', 'same_b_total': 5}, {'b': 1, 'id': '<some uuid2>', 'same_b_total': 5}, {'b': 2, 'id': '<some uuid3>', 'same_b_total': 3}, ...]
Table example:
id(uuid)| b
uuid-1 | 1
uuid-2 | 1
uuid-3 | 2
uuid-4 | 1
uuid-5 | 1
uuid-6 | 1
uuid-7 | 2
uuid-8 | 2
bandsame_b_totalis related? Can you give us some real example scenario?same_b_totalreflect total count of sameb. There is 5bwith value = 1 and 3bwith value = 2