code: def render_q(q, qn, connection):
      
      joinstr = u' {} '.format(q.connector)
      conditions = []
      params = []
  
      if DJANGO_MAJOR == 1 and DJANGO_MINOR == 7:
          # in Django 1.7 WhereNode.as_sql expects `qn` to have a `compile`
          # method (i.e not really expecting a quote names function any more
          # they are expecting a django.db.models.sql.compiler.SQLCompiler)
          try:
              qn = qn.__self__
          except AttributeError:
              pass
  
      for child in q.children:
          if isinstance(child, Q):
              # recurse
              condition, child_params = render_q(child, qn, connection)
              conditions.append(u'({})'.format(condition))
              params.extend(child_params)
          else:
              try:
                  # Django 1.7
                  child, joins_used = child
              except TypeError:
                  # Django 1.6
                  pass
              # we expect child to be a WhereNode (see transform_q)
              condition, child_params = child.as_sql(qn, connection)
              params.extend(child_params)
              conditions.append(condition)
      rendered = u'({})'.format(joinstr.join(conditions))
      if q.negated:
          rendered = u'NOT {}'.format(rendered)
      return rendered, params
docs: Renders the Q object into SQL for the WHEN clause.
  
      Uses as much as possible the Django ORM machinery for SQL generation,
      handling table aliases, field quoting, parameter escaping etc.
  
      :param q: Q object representing the filter condition
      :param qn: db specific 'quote names' function that was passed into
          SQLAggregate.as_sql method by Django
      :param connection: Django db connection object that was passed into
          SQLAggregate.as_sql method by Django
  
      :returns:  (SQL template str, params list) tuple