2017-03-16 56 views
0

我有一個包含一個Int能力屬性,和FK關係管理利用模型,如下所示:如何使用註解針對模型屬性過濾Django ORM查詢?

class Container(Model): 
    objects = ContainerManager() 

    capacity = PositiveSmallIntegerField(default=1) 



class Item(Model): 
    container = ForeignKey('Container', related_name='items' 

經理需要找到有餘力即有關聯的項目的數量的容器容器。我使用的是註釋計數的項目,但無法工作,如何與容量,即類似這樣的東西比較這..

get_queryset().annotate(utilisation=Count('items').filter(capacity__gt=utilisation) 

這可能在一個查詢?

回答

0

所以,我回答我的問題的情況下,它可以幫助別人。該解決方案是使用的F()的表達..

get_queryset().annotate(utilisation=Count('items').filter(capacity__gt=F(utilisation)) 
相關問題