title: how to filter a model objects by related table's foreign key in django REST
body: <p>I want to make an API for a restaurant, and my <code>models.py</code> is like this:</p>
  
  <pre><code>class Table(models.Model):
      id = models.AutoField(primary_key=True)
      name = models.CharField(max_length=30, null=False, blank=False)
      is_free = models.BooleanField(default=True)
  
      def __str__(self):
          return '%s %s %s' % (self.id, self.name, self.is_free)
  
  
  class Order(models.Model):
      order_id = models.AutoField(primary_key=True)
      table_id = models.ForeignKey(Table, on_delete=models.CASCADE)
      total_price = models.IntegerField()
      factor_id = models.IntegerField()
  
      def __str__(self):
          return '%s %s %s' % (self.order_id, self.table_id.id, self.total_price)
  
  
  class OrderDetail(models.Model):
      order_detail_id = models.IntegerField(primary_key=True)
      order_id = models.ForeignKey(Order, on_delete=models.CASCADE)
      food_id = models.ForeignKey(Food, on_delete=models.CASCADE)
      amount = models.IntegerField()
      price = models.IntegerField()
  
      def __str__(self):
          return '%s %s %s' % (self.order_detail_id, self.order_id, self.food_id.name)
  </code></pre>
  
  <p>When an Order with a Table created, some OrderDetails should add to it.</p>
  
  <p>I want to filter all OrderDetail objects that are for a specific Table and are for the latest Order(I mean all OrderDetail objects that are active now). The client request URL with filter contains <code>table_id</code> and not <code>order_id</code>. How can I filter OrderDetail objects by the table that have it?</p>
  
  <blockquote>
    <p>Each table can only have one order in a moment, When an Order creates with a specific Table, the <code>is_free</code> will set to False. So the <code>table_id</code> field of Order class should be <code>models.ForeignKey</code> not <code>models.OneToOneField</code>, I think so! :) If it's wrong please tell me.</p>
  </blockquote>
  
  <p>Thanks in advance.</p>