title: Django ORM returning stale data, possible race condition
body: <p>Consider a Django application with a single RESTful API that creates objects (using Django REST Framework). As part of this API, I do some validation to make sure the creation calls are idempotent, such that if you call the creation API twice, the first will succeed, and the second will fail with a custom error code.</p>
  
  <p>I have a scenario for testing this API which intermittently fails in the following way:</p>
  
  <ol>
  <li>First API call, success, returns 201 -> object has <em>supposedly</em> been created</li>
  <li>Immediately after response, second API call is made</li>
  <li>Validation logic calls <code>MyModel.objects.get(some_field=some_value)</code> to check if this is a duplicate call or not</li>
  <li><strong>No such object is found, despite being created in step 1</strong>, thus a duplicate object is created</li>
  <li>When inspecting the admin/querying the model, both objects can be seen.</li>
  </ol>
  
  <p>Some more data:</p>
  
  <ul>
  <li>There is no explicit caching on this model, or any other caching involved in this process.</li>
  <li>I am unable to reproduce this locally</li>
  <li>on my deployment setup there is about a 5% failure rate for this possible race condition.</li>
  <li>Both local and deployment use PostgreSQL.</li>
  <li>Deployment environment does have general caching enabled, but when enabling cache locally still no repro.</li>
  </ul>
  
  <p>What might be causing this race condition? Does Django ORM have any failure modes where I might be getting stale data? Is there any way I can defensively protect the validation from getting stale data?</p>