Wednesday, February 1, 2012

How to get unique results from fields in sliced QuerySet ?

Hi all,

I have models called Books.

To get unique results from Books, usually I use order_by().values('field').
For example, I want unique writter :

Books.objects.filter(created__year=2012).order_by('writter').values('writter').distinct()

But, how to get unique results from sliced Queryset ?

books = Books.objects.filter(created__year=2012)[:5]

# Doesnt' works , Can reorder a query once  a slice has been taken
unique = books
.order_by('writter').values('writter').distinct()

# Doesnt' works, it will show all queryset (not unique)
unique = books.values('writter').distinct()

# Also doesn't works
unique = books.annotate().values('writter').distinct()
unique = books.values('writter').distinct().annotate().

Anyone have this problem ? Is it possible to get unique results from sliced queryset ?


Thanks

Bashlook


No comments:

Post a Comment