我已经在trunk中尝试了django 1.4中的prefetch_related(),并且无法使其预取反向查找.
我的简化模型(每本书都有很多价格):
class Book(models.Model):
# some fields
class Price(models.Model):
book = models.ForeignKey(Book)
Run Code Online (Sandbox Code Playgroud)
我的观点是:
books = Book.objects.prefetch_related('price')
Run Code Online (Sandbox Code Playgroud)
然后,我收到了AttributeError消息:
AttributeError: Cannot find 'price' on Book object, 'price' is an invalid parameter to prefetch_related()
Run Code Online (Sandbox Code Playgroud)
如何使它工作?谢谢.
可以在此处找到问题:http:
//code.google.com/codejam/contest/dashboard?c = 975485#s = p3
我不明白为什么
answer = no. of elements that are not in the correct position
例如,假设我必须对此数组进行排序:
3 1 2
所以我这样想:
Array: 3 1 2 1st: freeze 2 to sort 1 (take 2 hits) Array: 1 3 2 2nd: freeze 1 to sort 2 and 3 (take another 2 hits)
因此,我的回答是4,但正确答案是3.
有人能澄清这个问题吗?