Re: Strange query when using annotate and count

2017-12-06 Thread Cristiano Coelho
Update. Found a work around that gets rid of the unnecessary group by (and hence speeds up the query, from ~200ms to ~100ms in my use case). Simply adding a .values('pk'). Bad thing, every model needs a custom manager and it will still use an inner query, still trying to figure out the side effe

Re: Strange query when using annotate and count

2017-12-06 Thread Cristiano Coelho
After testing for a while, there really doesn't seem to be a good way to do this. . Annotation is required since the query is filtered based on the annotated value, so any attempt to clear annotations would fail. Although a lookup could be used for the filtering. But a custom lookup is not poss

Re: Strange query when using annotate and count

2017-11-24 Thread Cristiano Coelho
Hello Simon, You are right, the score is really not meant to be attached so a custom lookup might work, if it wasn't for the issue that an order_by clause would fail without the annotation. So it seems it's either update and duplicate a lot of code or find a very ugly work around, I was hopin

Re: Strange query when using annotate and count

2017-11-24 Thread Simon Charette
Hello Cristiano, If you are solely using this annotation for querying purpose, that means you are not expecting a `score` attribute to be attached to your `Vulnerability` instances, you could try defining your function as a lookup on CharField/TextField instead. You could then use it only when

Re: Strange query when using annotate and count

2017-11-24 Thread Cristiano Coelho
Hello Simon, That private API is good to know, but now that I think of it would still not work for me, since my queryset is passed to a paginator and that's the class that does both the count and actual queryset execution, so need a queryset that can have both the annotation but also clears it

Re: Strange query when using annotate and count

2017-11-23 Thread Simon Charette
Hello Cristiano, > Isn't there a way (even if it's hackish) to simply clear the annotations (and order by)? I know querysets are smart enough to not include the order by clause if there's a count. The ordering is cleared on `count()` because it shouldn't interfere with the result in any way. Y

Re: Strange query when using annotate and count

2017-11-23 Thread Cristiano Coelho
Hello Simon, thanks for the response. The above code is just an example, the reason behind the annotate because there's some complicated code that builds a queryset and annotates it so it can easily be reused. It works fine 99% of the time except when there's a count involved and it ends up bei

Re: Strange query when using annotate and count

2017-11-23 Thread Simon Charette
Hello Cristiano, I understand your frustration but please avoid using the developer mailing list as a second tier support channel. I suggest you try the IRC #django channel if you need to want to get faster support. What's happening here is that annotate() really means "select this field" whil

Re: Strange query when using annotate and count

2017-11-21 Thread Cristiano Coelho
Hmm, should I try with the dev mailing list? Guess it's something no one faced before? El martes, 14 de noviembre de 2017, 22:54:23 (UTC-3), Cristiano Coelho escribió: > > I'm getting some very odd query when combining annotate with count. See > the following: > > >>> q = >> Vulnerability.obje

Strange query when using annotate and count

2017-11-14 Thread Cristiano Coelho
I'm getting some very odd query when combining annotate with count. See the following: >>> q = > Vulnerability.objects.annotate(score=WordTrigramCustomSimilarity('test','summary')) > >>> q.count() > 3094 > >>> print connection.queries[-1] > 'SELECT COUNT(*) > FROM ( > SELECT "vulnerabilitie