I have a database structure like as below:
id listing sequence result
1 1 0 hfdn
2 1 1 hkoq
3 1 2 qaz
4 2 1 bnm
5 3 1 h456
6 3 2 vnm
7 4 0 45hv
8 4 1 1lc0
I want to get each group of listing whose sequence is the smallest, the example result is as below:
{listing:1, sequence:0}, {listing:2, sequence:1}, {listing:3, sequence:1}, {listing:4, sequence:0}
and their matching result.
PS: some sequence is start from 0, some is start from 1, or even more.
you need to group_by listing
and get minimum sequence
value. If your model name is MyModel
, then this should work fine:
MyModel.objects.values('listing').annotate(min_sequence=Min('sequence'))
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.