I have the following models
class Club(Adresse):
...
class Member(Adresse):
club = ForeignKey(Club, on_delete=models.CASCADE)
type = PositiveSmallIntegerField(default=1)
points = PositiveSmallIntegerField(default=0)
I want to collect some statistics and I used annotate to count the numbers of members with several types and points. Lastly, I want to see the ratio between members of type 1 and 2. The ratio is always returned as 0.0
, even if all other values are greater than 0. Why?
type1 = Count("member", filter=Q(member__type=1))
type2_gt0 = Count("member", filter=Q(member__type=2) & Q(member__points__gt=0))
type2_eq0 = Count("member", filter=Q(member__type=2) & Q(member__points=0))
statistics = Club.objects.annotate(
all=Count("member"), # Works!
type1=type1, # Works!
type2_gt0=type2_gt0, # Works!
type2_eq0=type2_eq0, # Works!
ratio=ExpressionWrapper(
F("type1") / (F("type2_gt0") + F("type2_eq0")),
output_field=FloatField()
), # Always 0.0
)