title: Django get a random object
body: <p>I am trying to get a random object from a model A</p>
  
  <p>For now, it is working well with this code:</p>
  
  <pre><code>random_idx = random.randint(0, A.objects.count() - 1)
  random_object = A.objects.all()[random_idx]
  </code></pre>
  
  <p>But I feel this code is better:</p>
  
  <pre><code>random_object = A.objects.order_by('?')[0]
  </code></pre>
  
  <p>Which one is the best? Possible problem with deleted objects using the first code? Because, for example,  I can have 10 objects but the object with the number 10 as id, is not existing anymore? Did I have misunderstood something in A.objects.all()[random_idx] ?</p>