title: In Django, how to achieve repeatable reads for a transaction?
body: <p>I have a function, that does multiple queries on the same dataset and I want to ensure all the queries would see exactly the same data.</p>
  
  <p>In terms of SQL, this means REPEATABLE READ isolation level for the databases that support it. I don't mind having higher level or even a complete lockdown if the database isn't capable.</p>
  
  <p>As far as I see, this isn't the case. I.e. if I run something like this code in one Python shell:</p>
  
  <pre><code>with transaction.atomic():
      for t in range(0, 60):
          print("{0}: {1}".format(t, MyModel.objects.count()))
          time.sleep(1)
  </code></pre>
  
  <p>As soon as I do <code>MyModel.objects.create(...)</code> in another, the value seen by the running loop increase immediately. Which is exactly what I want to avoid. Further tests shows the behavior matches READ COMMITTED level, which is too lax for my tastes.</p>
  
  <p>I'd also want to stress the point, I want stricter isolation level only for a single function, not for the whole project.</p>
  
  <p>What are my best options to achieve this?</p>
  
  <p>In my particular case, the only database I care of is PostgreSQL 9.3+, but I also want some compatibility with SQLite3 in which case even completely locking the whole database is okay with me. Yet, obviously, the more general the solution is, the more preferred it is.</p>