【发布时间】:2014-08-22 02:50:53
【问题描述】:
我正在尝试通过检查对象是否在这些对象的列表中来过滤查询集。
employee_list = [<Employee: A>, <Employee: B>, <Employee: C>]
qs = Employee.objects.filter(id__in=employee_list, [other_filters])
上面运行后,qs 是一个空列表。我在想我可以做一个新的清单,比如
employee_ids = [emp.id for emp in employee_list]
qs = Employee.objects.filter(id__in=employee_ids, [other_filters])
我还没有对这种方法进行基准测试,但我想性能可能会受到影响。或者,我可以在之后与列表相交,例如:
qs = Employee.objects.filter([other_filters])
filtered_qs = [emp for emp in employee_lids if emp in qs]
但是,我认为这样对性能的影响会更糟。
最好/最快的方法是什么?谢谢。
【问题讨论】:
标签: python django django-queryset intersection django-filter