Skip to content

Instantly share code, notes, and snippets.

@evansd
Created November 13, 2014 14:10
Show Gist options
  • Select an option

  • Save evansd/6127048f9efcf7889b96 to your computer and use it in GitHub Desktop.

Select an option

Save evansd/6127048f9efcf7889b96 to your computer and use it in GitHub Desktop.
def limit_with_cte(queryset, limit=None):
if limit is None:
return queryset
sql, params = queryset.query.sql_with_params()
cte_query = 'WITH cte_table_a4eb61 AS ({}) '\
'SELECT * FROM cte_table_a4eb61 LIMIT {:d}'.format(
sql, int(limit))
return queryset.raw(cte_query, params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment