def counts_of_boolean_variables(cls, lga): result = cls.counts_by_variable(lga) for sector, d in result.items(): for k, v in d.items(): variable = Variable.get(k) if variable.data_type != 'boolean': del d[k] return result
def counts_by_variable(cls, lga): records = cls.objects.filter(facility__lga=lga).values('facility', 'variable', 'float_value', 'boolean_value', 'string_value', 'facility__sector').annotate(Max('date')).distinct() def infinite_dict(): return defaultdict(infinite_dict) result = infinite_dict() for d in records: variable = Variable.get(d['variable']) value = '%s_value' % variable.data_type if d[value] in result[d['facility__sector']][d['variable']]: result[d['facility__sector']][d['variable']][d[value]] += 1 else: result[d['facility__sector']][d['variable']][d[value]] = 1 return result