Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django query to get a unique set based on a particular column's value

Hope this makes sense...

Is there a simple way to return a set of values from a table based on a single column's values being distinctly unique? What I'm hoping for is something like:

SegCode.query.filter(ref.unique()).only('ref')

That's not real code, but I was hoping there was some simple function out there that will accomplish this...


E.g. Table might look like:

1 | abc | 123 | AAA  
2 | def | 456 | AAA  
3 | ghi | 789 | BBB  
4 | jkl | 012 | CCC  
5 | mno | 345 | CCC  
6 | pqr | 678 | CCC  
7 | stu | 901 | DDD  
8 | vwx | 234 | DDD  

So, I'd like a set that comes back with: [AAA, BBB, CCC, DDD]

like image 580
kafuchau Avatar asked Mar 26 '10 20:03

kafuchau


1 Answers

SegCode.objects.values_list('ref', flat=True).distinct()

I think this is what you're after, your question isn't really that clear

like image 99
priestc Avatar answered Oct 13 '22 06:10

priestc