Django - queryset with optional method returns empty, count () says otherwise

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.

+4
source share
1 answer

I think I just realized the problem. Thanks, Alex, whose comment triggered the thought:

The PortfolioItem model has the current_value and current_gain properties, which I am trying to replace with computed SQL fields. It was my mistake to name one of the optional () select fields 'current_value' methods without deleting the property, as this led to the model having two fields with the same name. When I finished this overlap, everything became good. Lesson learned.

+1
source

All Articles