2009-10-31 74 views
4

這是一個相關的經理,我寫道:Django的 - queryset的額外方法返回空,計數()說,否則

class PortfolioItemManager(models.Manager): 

    use_for_related_fields = True 

    def extended(self): 

     return self.extra(select = {'current_price':'current_price', 'current_value':'current_price*quantity', 'gain':'current_price*quantity - cost'}, 
         tables = ['pm_core_contract', ], 
         where = ['pm_core_contract.id = pm_core_portfolioitem.contract_id', ] 
       ) 

下面是樹樁我的結果:

In [10]: PortfolioItem.objects.extended() 
Out[10]: [] 
In [11]: PortfolioItem.objects.extended().count() 
Out[11]: 402 

從計數結果() 是正確的。我在這裏錯過了什麼?

編輯:生成的SQL是正確的,可以直接對db執行。

EDIT2:該問題源自最後2個選擇參數,其特徵是算術運算。

+1

請給我們您的PortfolioItem結構 – 2009-11-01 09:47:13

回答

1

我想我剛纔已經知道了這個問題。感謝Alex,他的評論引發了這個想法:

PortfolioItem模型具有屬性current_valuecurrent_gain,我一直試圖用計算的SQL字段替換。這是我的錯誤,在不刪除屬性的情況下命名其中一個extra()方法的選擇字段'current_value',因爲導致該模型具有兩個具有相同名稱的字段。當我消除了重疊時,一切都變好了。經驗教訓。