Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何基于django中的父查询集创建预回迁查询集_Django_Django Queryset_Prefetch - Fatal编程技术网

如何基于django中的父查询集创建预回迁查询集

如何基于django中的父查询集创建预回迁查询集,django,django-queryset,prefetch,Django,Django Queryset,Prefetch,下面是一个场景,一个包含多个投标的项目模型 Class Project(models.Model): user = models.ForeignKey() Class Bid(models.Model): project = models.ForeignKey(Project, related_name='bids') 当我们查询项目时,我们希望预取项目的投标 Project.objects.filter(whatever condition).prefetch_relate

下面是一个场景,一个包含多个投标的项目模型

Class Project(models.Model):
    user = models.ForeignKey()

Class Bid(models.Model):
    project = models.ForeignKey(Project, related_name='bids')
当我们查询项目时,我们希望预取项目的投标

Project.objects.filter(whatever condition).prefetch_related(
     Prefetch('bids', queryset=Bid.objects.all())
)
在这里,我们只想获取属于筛选项目的投标,而不是所有投标,我们如何指定?我期待着像这样的事情

queryset=Bid.objects.filter(project=project?)... 
谢谢

Project.objects.filter(whatever condition).prefetch_related(
     Prefetch('bids', queryset=Bid.objects.all())
)
这个看起来不错。Django将只负责为您获取相关的出价。请注意,在这种情况下,您不需要
预取
。你可以做:

Project.objects.filter(whatever condition).prefetch_related('bids')
如果要过滤查询集,则
预取
非常有用,例如:

Project.objects.filter(whatever condition).prefetch_related(
     Prefetch('winning_bids', queryset=Bid.objects.filter(status='WINNING'))
)