美文网首页
aggregation and join

aggregation and join

作者: xncode | 来源:发表于2017-09-12 11:27 被阅读0次

可以直接使用双下划线来获得所关联的object的属性

Store.objects.annotate(min_price=Min('books__price'), max_price=Max('books__price'))

Store.objects.aggregate(min_price=Min('books__price'), max_price=Max('books__price'))

Store.objects.aggregate(youngest_age=Min('books__authors__age'))

这样的话 django会把Store和Book通过多对多关系join。

此外,可以使用逆向的指定,例如book有指向publisher的ForeignKey,但是在publisher中也可以用

Publisher.objects.annotate(Count('book'))

此外many-to-many关系也适用

相关文章

网友评论

      本文标题:aggregation and join

      本文链接:https://www.haomeiwen.com/subject/ldccsxtx.html