The reason .count() doesn't work there is because count() runs a select 
count(*) query, but that can't run correctly with .raw().
The way to get the count is to cast the Resulting RawQuerySet to a list and 
run len() on it. This will result in the RawQuerySet getting evaluated 
however. 

Alternatively, you could run the query twice using a django.db.connection 
object to run 'select count(*) from table s where s.col3 = to_char(%s)', 
[xxx] to get the count.

http://stackoverflow.com/questions/3037273/get-number-of-results-from-djangos-raw-query-function
http://stackoverflow.com/questions/2317452/django-count-rawqueryset

On Monday, March 10, 2014 6:21:27 AM UTC-5, bikeridercz wrote:
>
> Dear colleagues,
>
> please help, it seem that count method used in template does not work for 
> collections of records populated in views via direct "raw" query.
>
> *Example in view:*
>
> recordset = ASSETS.objects.raw('select s.col1, s.col2 from table s where 
> s.col3 = to_char(%s)', [xxx])
>
>
> *Example in template:*
>
> {% with total=recordset.count %}
>
> <p class="float-right">{{ total|default:"0" }} record{{ total|pluralize }} 
> found</p>
>
> {% endwith %}
>
>  
> Select returns approx. 20 rows, they are successfully populated into 
> "recordset", they are iterable, can be accessed and displayed with no 
> problem.
>
> However "recordset.count" returns nothing.
>
> In case that the recordset is populated in standard way through 
> ASSETS.object.filter, the "count" method works fine. Unfortunately, 
> "objets.filter" can not be used in this case.
>
> Thanks for help.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b66c3df8-f251-40b8-a42e-b78f6e614344%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to