2015-08-26 98 views
0

以下函数在我的django项目的views.py中定义。我遇到的唯一问题是按小写顺序(即Lower())和相反顺序(即'-title'而不是'title')排序所有书籍。我能够通过其中一个订购,但不能同时订购。以小写颠倒顺序排列QuerySet

我得到以下错误:

Cannot resolve keyword '-title' into field. Choices are: author, date_modified, title

def book_list_title(request): 

    all_entries = Book.objects.all().order_by(Lower('-title')) 
    books_list=[] 

    //Do stuff to create a proper list of books 

    return render(request,'books_app/books_list.html', {'books_list':books_list}) 

回答

0

尝试使用Query Expressions

from django.db.models import Func, F 

Book.objects.all().annotate(title_lower=Func(F('title'), function='LOWER')).order_by('-title_lower')