1

I have Shop model class. I am passing a QueryString to raw Django ORM to get all non duplicate records + 1 record from duplicates.

Below is the query:

   qs = Shop.objects.raw(select a.* from customers a INNER JOIN (SELECT city ,MIN(id) as id FROM customers GROUP BY city) AS b ON a.city = b.city AND a.id = b.id)

Here I get a Raw Query Set.I want to know the count of queried result. How I can proceed?

For Pagination:

page_object = Pagination(qs, 2)
page_count = page_object.num_pages

After executing above line i am getting an error as:

TypeError: object of type 'RawQuerySet' has no len()

Can someone have any idea?How can i apply pagination to my raw queryset.

Whatever things i am trying is because i am using MySQL as DB.

"pagination = Paginator(qs, records_per_page) 
totalPageCount = pagination._count = len(list(qs))"

I got the error on 2nd line as You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY created' at line 1

One thing i just want to make sure is it possible to count the records from a raw QuerySet If so then how?

danny
  • 983
  • 1
  • 7
  • 13
  • Not entirely sure but I think you either need to make another raw query to get the count or you could not use a raw query with some overhead but overcome this. – Wtower Apr 26 '16 at 07:51
  • 1
    Possible duplicate of [django pagination and RawQuerySet](http://stackoverflow.com/questions/5152984/django-pagination-and-rawqueryset) – e4c5 Apr 26 '16 at 07:54
  • After applying same concept **"pagination = Paginator(qs, records_per_page) totalPageCount = pagination._count = len(list(qs))"** I got the error on 2nd line as `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY created' at line 1` – danny Apr 26 '16 at 08:52
  • Then update the question. State clearly that you tried the solution that was pointed as a duplicate question and got this new error. – e4c5 Apr 26 '16 at 10:49
  • How can i count the records based on raw query set? – danny Apr 26 '16 at 11:25
  • Question Updated..Please see – danny Apr 26 '16 at 11:30

0 Answers0