title: Django: efficient filtering in the ORM across ForeignKeys?
body: <p>I'm trying to construct a moderately complex filter in the Django ORM, and I'm not sure if I could do it more efficiently. </p>
  
  <p>My app lets users search for shoes in a particular size. So if they select a size (or multiple sizes), I look for all the shoes available in that size. </p>
  
  <p>This is my model structure. There's an object for each shoe (<code>Shoe</code>), an object for each size (<code>ShoeSize</code>) (standardised across manufacturers), and an object (<code>ShoeSizeAvailable</code>) that only gets created if a particular shoe is available in a particular size. </p>
  
  <pre><code>class Shoe(ClothingItem):
    price = models.FloatField(null=True,blank=True,db_index=True) ... 
  
  class ShoeSize(models.Model):
    code = models.CharField(max_length=8) ...
  
  class ShoeSizeAvailable(models.Model):
    shoe = models.ForeignKey(Shoe)
    size = models.ForeignKey(ShoeSize)
  </code></pre>
  
  <p>And this is how I currently do the filtering: </p>
  
  <pre><code>kwargs = {}
  args = ()
  if query['price_from']: 
    kwargs['price__gte'] = float(query['price_from'][0])
  # Lots of other filters here... 
  results = Shoe.objects.filter(*args, **kwargs)
  
  if query['size']: 
      # The query is a list like [6, 6.5]
      # Get all potential ShoeSizeAvailable matches. 
      sizes = ShoeSizeAvailable.objects.filter(size__code__in=query['size'])
      # Get only the ones that apply to the current shoe. 
      shoe_ids = sizes.values_list('shoe', flat=True)
      # Filter the existing shoe results for these values.  
      results = results.filter(id__in=shoe_ids)
  </code></pre>
  
  <p>Is this the most efficient way? I'm concerned that <code>__in</code> queries might be inefficient with long lists like these. </p>