title: Django: How to create a queryset from raw?
body: <p>I have a query which seems to be hard to do using Django conventions by using <code>filter()</code> or <code>extra()</code>. In that way i think that performing a raw SQL query and transform that into queryset is a better solution for me. </p>
  
  <p>For that reason i started to search how to perform a raw SQL query and how to create a queryset from raw.</p>
  
  <p>i found this solution from <a href="https://gist.github.com/carymrobbins/8477219" rel="nofollow">CaryMRobbins</a> to convert raw to queryset. So i decided to put this in my <code>models.py</code>:</p>
  
  <pre><code>from django.db import connection, models
  
  class MyManager(Manager):
  
  def raw_as_qs(self, raw_query, params=()):
  """Execute a raw query and return a QuerySet. The first column in the
  result set must be the id field for the model.
  :type raw_query: str | unicode
  :type params: tuple[T] | dict[str | unicode, T]
  :rtype: django.db.models.query.QuerySet
  """
    cursor = connection.cursor()
    try:
      cursor.execute(raw_query, params)
      return self.filter(id__in=(x[0] for x in cursor))
    finally:
      cursor.close()
  
  class MyModel(models.Model):
    objects = MyManager()
  </code></pre>
  
  <p>But it seems to not work properly because it returns a queryset empty. </p>
  
  <p>In <code>views.py</code> i add the following script:</p>
  
  <pre><code>raw =  ''' My huge 
             query working'''
  queryset = MyModel.objects.raw_as_qs(raw)
  print queryset # result is empty list : []
  </code></pre>
  
  <p>Following this approach what am i missing?</p>
  
  <p>I'm having some difficult to understand how <code>return self.filter(id__in=(x[0] for x in cursor))</code> works to convert into qs.</p>