使用相关名称反转FK查找

时间:2013-01-15 23:33:12

标签: python django

我在使用以下反向FK查找时遇到问题:

class OrderItem(models.Model):
    order = models.ForeignKey(Order, related_name='items')
    title = models.ForeignKey(Title, related_name='order_items')

class OrderItemDelivery(models.Model):
    order_item = models.ForeignKey(OrderItem, related_name='deliveries')
    stage = models.CharField(max_length=20)

我如何执行以下操作:OrderItem.objects.filter(order_item_delivery__stage='Beginning')

2 个答案:

答案 0 :(得分:4)

由于您已将deliveries指定为related_name,请使用它:

OrderItem.objects.filter(deliveries__stage='Beginning')

答案 1 :(得分:2)

 OrderItem.objects.filter(deliveries__stage='Beginning')