MODELS
class ModelA(models.Model):
name = models.CharField()
class ModelB(models.Model):
MY_CHOICES = (
('X', 'X'),
('Y', 'Y'),
('Z', 'Z'),
)
modela = models.ForeignKey(ModelA, on_delete=models.CASCADE)
txt_1 = models.CharField(choices=MY_CHOICES)
txt_2 = models.CharField(choices=MY_CHOICES)
Given the simplified example above, how could I count how many times each of the choices values has been recorded given that there are Two fields that need to be counted?
Ideally the result would be something along the lines of:
{'X': 15, 'Y': 27, 'Z': 89}
I have tried the following, however in my real model, I have around 20 fields to count across and this is not giving the results I was hoping for:
ModelA.objects.values('modelb__txt1', 'modelb__txt2').annotate(Count('modelb__txt1', 'modelb__txt2'))
I have previously created huge dictionaries and manually sorted/counted the values, but this is now unmanageable and ugly.