in my database there is a table that holds some user information. The table is called UserBalance
and fields include user
, credits_in
and credits_out
and remark
(among others)
I am trying to sum the credits_in
for a certain user but I get different sums for different cases. Take a look at this:
>>> cg = UserBalance.objects.filter(user=ranked_user).filter(remark__icontains='credits g').aggregate(sum_in=Sum('credits_in'))
>>> cg
{'sum_in': 35.85}
>>> cg = UserBalance.objects.filter(user=ranked_user).filter(Q(remark='credits gained') or Q(remark='credits given')).aggregate(sum_in=Sum('credits_in'))
>>> cg
{'sum_in': 26.16}
>>> cg = UserBalance.objects.filter(user=ranked_user).filter(Q(remark='credits given') or Q(remark='credits gained')).aggregate(sum_in=Sum('credits_in'))
>>> cg
{'sum_in': 9.69}
in the first case I used i_cointains
, in the second and third cases I use a Q() but with its terms switched.
Can someone explain what is the difference between the 2nd and the 3rd case?