title: How to persist a query between flask page requests?
body: <p>I need to run the same exact query on multiple page requests.
  The first page renders the items, the second page exports the items to excel.</p>
  
  <p>Storing the query directly on session fails because <code>BaseQuery</code> is not JSON serializable:</p>
  
  <pre><code>session['previous_query'] = SomeModel.query
  </code></pre>
  
  <p>The next option is to store the query as a string:</p>
  
  <pre><code>session['previous_query'] = str(SomeModel.query)
  </code></pre>
  
  <p>This works but I would now need to run session.execute:</p>
  
  <pre><code>db.session.execute(session['previous_query'])
  </code></pre>
  
  <p>And that does not give me ORM objects but plain dicts without relationships.</p>
  
  <p>Finally I can store only the ids, but that would require me to run the query on both ends multiple times and would not preserve the ordering I need.</p>
  
  <p>Any suggestions?</p>