-1

I am trying to display distinct or unique values from my column/field category

The function from views.py:

def category(request, book_category):
    latest_book_list = Books.objects.all().order_by('id')
    return render_to_response('books/category.html', {'latest_book_list': latest_book_list})

I would like the line: latest_book_list = Books.objects.all().order_by('id')

To perform the mysql query: mysql> select distinct category from books;

I have tried using the Books.objects.filter(category=book_category) but it returns blank.

Any suggestions?

Jorge Aranda
  • 2,050
  • 2
  • 20
  • 29
user1786284
  • 75
  • 2
  • 11
  • Duplicate of http://stackoverflow.com/questions/3852104/select-distinct-individual-columns-in-django ? – j0ker Nov 09 '12 at 20:04

1 Answers1

1

Try this...

cateogries = Books.objects.values_list('category', flat=True).distinct()
latest_book_list = Books.objects.filter(category__id__in = categories)
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62