2013-01-16 43 views
0

我試着用下面的語句在聲明和值誤差和Django

top_book = Borrow.objects.filter(beginDate__gte=str(weekAgoDate)).values('book').order_by().annotate(num_borrow=Count('book')).order_by('-num_borrow')[:5] 

做出COUNT GROUP BY和Django但是當我嘗試在模板中訪問數據它不給作爲輸出任何東西

{% for cont in top_book %}   
<tr> 
<td><a href="/toplibrary/bookInfo/{{ cont.book.idBook }}/">{{ cont.book.title }}</a></td><td>{{ cont.book.isbn }}</td><td>{{ cont.book.author }}</td><td>{{ cont.book.editor }}</td>  

</tr>{% endfor %} 

所以我試圖做一個IN語句類圖書獲得所有的信息,我需要我的書

top_books=Book.objects.filter(idBook__in=top_book) 

但我得到這個錯誤

"This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'" 

有人可以提出一個解決方案嗎?

class Book(models.Model): 
    idBook = models.IntegerField(primary_key=True) 
    title = models.CharField(max_length=255) 
    isValid = models.BooleanField(default=True) 
    linkDescr = models.CharField(max_length=255) 
    editor = models.CharField(max_length=255) 
    author = models.CharField(max_length=255) 
    isbn = models.BigIntegerField() 
    categories = models.ManyToManyField(Category, verbose_name="Categories") 
    position = models.ForeignKey(Position, verbose_name="Position") 
    addedDate = models.DateTimeField() 

class Borrow(models.Model): 
    beginDate = models.DateField() 
    renewed = models.BooleanField(default=False) 
    user = models.ForeignKey(User, verbose_name="BorrowHasUser") 
    book = models.ForeignKey(Book, verbose_name="BorrowHasBook") 
    returnDate = models.DateField(blank=True, null=True) 

回答

0

等等,你是否試圖獲得最多借書?

這也許會更有意義:

top_book = Book.objects.annotate(num_borrow=Count('borrow')).order_by('-num_borrow')[:5] 

如果你有你的Borrow.book領域related_name集,用它來代替borrow

 

對不起,我先回答了自動駕駛。

+1

謝謝,但是如果我還想在模板中使用num_borrow屬性呢?有解決方案嗎? – user1922860

+0

對不起,編輯了我的答案。 –

+0

感謝您的幫助,但它不起作用。我沒有鏈接書籍借閱,但借用書籍。我通過課堂宣言編輯我的問題 – user1922860