Query help

18 views
Skip to first unread message

J.T.

unread,
Sep 20, 2021, 6:41:22 PM9/20/21
to Django users
I have a model called member that has 6  columns: ID, Value1, Value2, Value3, Value4, Value5

The values in columns Value1, Value2, Value3, Value4, Value5 are either Y or N.

I'm trying to query by ID to count the number of Y values for each member.

For example one row would be: 5, Y, N, Y, N, N

I would like to return: 2 (since that is the number of Y in that member's row)

I've tried using Aggregate, Annotate, F, Q, Sum, Count in different ways and I'm missing the mark. I've been through the docs and SO for about 2 hours.
I would post my errors but it's on my work computer and I have to post here from my personal laptop.

I feel like I may be over-thinking it but I can't quite figure it out. Any help would be greatly appreciated. 

Jason


Sean Collins

unread,
Sep 20, 2021, 7:17:05 PM9/20/21
to django...@googlegroups.com
Add a property to count Y for each column

Or add a computed field that tallies that on save 

For reference


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/597a0acf-65a0-4e9a-8a9f-b61892860789n%40googlegroups.com.

Lalit Suthar

unread,
Sep 21, 2021, 1:13:12 AM9/21/21
to django...@googlegroups.com
Sum, Count works when we try to get sum/count for a column,
for this case you can try something like this

def get_yes_count_by(id):
    user = User.objects.get(id=id)
    count = 0
    for val in user.__dict__.values():
        if val == "YES":
            count += 1
    return count 

J.T.

unread,
Sep 21, 2021, 10:08:56 AM9/21/21
to Django users
Makes sense, I probably should have realized this from the beginning.

Appreciate the help.

Reply all
Reply to author
Forward
0 new messages