简体   繁体   中英

Finding the distribution of a field in a list of objects?

I have a list of objects. Each object has a field called grade whose value is between 0 and 5. Now I want to see the distribution of this field across my list of objects. Is there any way to find it?

I know I can iterate over the whole objects and find it out but I don't want to do that.

As near as I can tell, using a table Table with a grade column you need something like this:

counts = Table.objects.annotate(count=Count("grade", distinct=True)

This adds a count attribute to each member of the counts query set, which you can access just like a regular database column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM