在django 1.2.1中可以将注解与defer/only结合使用吗?

2 投票
1 回答
633 浏览
提问于 2025-04-16 02:44

我有两个简单的模型:书籍(Book)和作者(Author)。

每本书都有一个作者,通过外键(foreign key)连接起来。

一切正常,直到我尝试在一个注解上使用defer/only。

authors=Author.objects.all().annotate(bookcount=Count('books'))

这样是可以的。查询看起来像这样:

select table_author.name, table_author.birthday, COUNT(table_book.id) as bookcount 
from table_book left outer join table_author on table_author.id=table_book.author_id 
group by table_author.id

非常简单——从作者那里选择所有信息,并额外选择书籍的数量。

但是当我这样做时,一切都变了:

simple=authors.defer('birthday')

现在,这个简单的查询看起来是这样的:

select COUNT(table_book.id) as bookcount from table_book left outer join 
table_author on table_author.id=table_book.author_id group by table_author.id

而且完全失去了额外的信息。这是怎么回事呢?

1 个回答

1

嗯,这看起来像是一个错误。已经有一个相关的记录,不过这段时间没有人关注。也许可以在django-developers的Google小组发个帖子,催促一下大家关注这个问题。

撰写回答