title: Django: filter a RawQuerySet
body: <p>i've got <a href="https://stackoverflow.com/questions/9102907/can-you-solve-this-simple-sql-query">some weird query</a>, so i have to execute raw SQL. The thing is that this query is getting bigger and bigger and with lots of optional filters (ordering, column criteria, etc.).</p>
  
  <p>So, given the this query:</p>
  
  <pre><code>SELECT DISTINCT Camera.* FROM Camera c
       INNER JOIN cameras_features fc1 ON c.id = fc1.camera_id AND fc1.feature_id = 1
       INNER JOIN cameras_features fc2 ON c.id = fc2.camera_id AND fc2.feature_id = 2
  </code></pre>
  
  <p>This is roughly the Python code:</p>
  
  <pre><code>def get_cameras(features):
    query = "SELECT DISTINCT Camera.* FROM Camera c"
    i = 1
    for f in features:
      alias_name = "fc%s" % i
      query += "INNER JOIN cameras_features %s ON c.id = %s.camera_id AND %s.feature_id = " % (alias_name,alias_name,alias_name)
      query += " %s "
      i += 1
    return Camera.objects.raw(query, tuple(features))
  </code></pre>
  
  <p>This is working great, but i need to add more filters and ordering, for example suppose i need to filter by color and order by price, it starts to grow:</p>
  
  <pre><code>#extra_filters is a list of tuples like:
  # [('price', '=', '12'), ('color' = 'blue'), ('brand', 'like', 'lum%']
  def get_cameras_big(features,extra_filters=None,order=None):
    query = "SELECT DISTINCT Camera.* FROM Camera c"
    i = 1
    for f in features:
      alias_name = "fc%s" % i
      query += "INNER JOIN cameras_features %s ON c.id = %s.camera_id AND %s.feature_id = " % (alias_name,alias_name,alias_name)
      query += " %s "
      i += 1
    if extra_filters:
      query += " WHERE "
      for ef in extra_filters:
        query += "%s %s %s" % ef #not very safe, refactoring needed
    if order:
      query += "order by %s" % order
  
    return Camera.objects.raw(query, tuple(features))
  </code></pre>
  
  <p>So, i don't like how it started to grow, i know <code>Model.objects.raw()</code> returns a RawQuerySet, so i'd like to do something like this:</p>
  
  <pre><code>queryset = get_cameras( ... )
  queryset.filter(...)
  queryset.order_by(...)
  </code></pre>
  
  <p>But this doesn't work. Of course i could just perform the raw query and after that get the an actual QuerySet with the data, but i will perform two querys. Like:</p>
  
  <pre><code>raw_query_set = get_cameras( ... )
  camera.objects.filter(id__in(raw_query_set.ids)) #don't know if it works, but you get the idea
  </code></pre>
  
  <p>I'm thinking that something with the QuerySet <strong>init</strong> or the cache may do the trick, but haven't been able to do it.</p>