views:

211

answers:

2

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]

+2  A: 
SegCode.objects.values_list('ref', flat=True).distinct()

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

nbv4
Haven't tried your code yet, but I just updated with some more info, hopefully clarifying what I'm trying to accomplish.
kchau
Ok then, according to your edit, my code is exactly what you want
nbv4
That only works if my SegCode class is a Django model, correct? What if my class was an Elixir Entity, meaning I had to use Querysets to do the query? I ask because I'm getting an error: type object 'SegCode' has no attribute 'objects'
kchau
If SegCode is an Elixir Entity, then the whole thing goes out the window. Elixir has nothing to do with django querysets. You'd have to create a separate question to address how to do it.
nbv4
Okay, well your answer works for a Django model. :)
kchau
A: 

kchau - Answer does not seem to work for a Django model QuerySet. I am trying it not too no avail.

Sean