How to get a django QuerySet of all ForeignKeys of all objects in a Queryset

前端 未结 3 1652
温柔的废话
温柔的废话 2021-02-03 12:27

I have a model (or actually 2 models, but the other is not that relevant)

class Foo(models.Model):
    ...
    bar = models.ForeignKey(Bar,
        ...
    )
         


        
3条回答
  •  暖寄归人
    2021-02-03 12:55

    foo_queryset = Foo.objects.filter(attr=value)
    referenced_bars = Bar.objects.filter(id__in=foo_queryset.values('bar_id'))
    

提交回复
热议问题