Here is the related manager I wrote:
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', ] )
Here are the results that haunt me:
In [10]: PortfolioItem.objects.extended() Out[10]: [] In [11]: PortfolioItem.objects.extended().count() Out[11]: 402
The result from count () is correct. What am I missing here?
EDIT: Generated SQL is correct and can be executed directly with db.
EDIT2: The problem is with the last 2 selection arguments, which contain arithmetic operations.
source share