Thursday, May 25, 2017

Re: Building correct queryset

On Monday 22 May 2017 15:26:59 Todor Velichkov wrote:

> Hello, Ð"митрий,

> you can try this one, but w/o further optimizations it may be a very

> slow query.

>

> qs = Product.objects.filter(

> #Where score is greater or equal

> #to the 4th max score from its group

> score__gte=Subquery(

> (Product.objects

> .filter(brand=OuterRef('brand'))

> .values('score')

> .order_by('-score')[3:4]

> )

> )

> ).order_by('-score')

 

Yeah, that's how I read it too. But the code says score is on Brand model, not Product. Which is correct?

--

Melvyn Sopacua

No comments:

Post a Comment