View, split and use second value

votes = (
    PeerMonitor.objects.filter(state=PeerMonitor.State.ONLINE)
    .values("platform")
    .annotate(cnt=Count("platform"))
    .order_by("-cnt")
)

so in the above the “platform” value looks something like value1;value2;value3; I need to only grab the value2 and count it vs the whole platform string. any easy way here to do that? i cant use split in a queryset, not sure how to go about it here.

Please provide an example if possible.

You could probably build an expression using the Substr and StrIndex database functions.

But, what I would really recommend is that if this is going to be a common operation, you would refactor your database to extract that data into separate fields - or possibly even a related model, and perform the data manipulations when the data is being inserted into the table.

great can you give me a example making some assumptions. that field can not be broken out, its a string field ; separated.

The examples of using those functions are in the docs.

Sure it can. How is this data getting entered into the database?

im looking for a hand with the existing data. and im looking for a hand in the form of an example. its a peer to peer blockchain database. spread out across 1000 nodes. So I have to deal with the data i have. If you can not assist with either I understand. Ill hope someone with those abilities can post.

I can assist, however, I’m not going to write your code for you. It’s up to you to give it a try to see what you can come up with. If you’re still having problems after you’ve made your attempt, I’ll be more than happy to help guide you through to a solution.

Thanks, yes i have tried already with no success… and only post in forums once i am totally stuck. hence my post here.

You’ve tried using those database functions?

Ok, so why don’t you post what you’ve tried, and we can move forward from there.

I have not, i do not understand how. hence an example for me to start from and learn from. I’m a visual learner with heavy ADHD reading is not the method i learn. if you don’t want to “write my code” write an example similar to what i am looking for, ill learn from it and write my own.

Again, the referenced docs show examples of using those functions.

thanks, i now understand why you have 10k+ posts with only 765 solutions. Figured i would try this forum again for help… but same person, same kind of “read the manual” response.

Best of luck to you!

Best of luck to me, yes.

Assuming that the platform field always contains a semicolon-separated string and you only want to count the occurrences of value2 , you can use Django’s Case and When expressions within the Count function to conditionally count instances of value2 .