简体   繁体   English

在django中从较大的查询集中排除查询集而不访问数据库

[英]Exclude a queryset from bigger queryset in django without hitting the database

I have a queryset of all objects of a model. 我有一个模型的所有对象的查询集。 Iterating over the objects, I am removing updating rows with value repetition in a column. 迭代对象,我正在删除列中具有值重复的更新行。 So without having to hit the database again, i want to remove the updated rows from the bigger queryset. 因此,无需再次访问数据库,我想从更大的查询集中删除更新的行。

What is the most efficient way to do this? 最有效的方法是什么?

You may need to provide more information about your specific use case, but in general, Django defers the evaluation of QuerySets until they are actually needed. 您可能需要提供有关特定用例的更多信息,但一般而言,Django推迟对QuerySets的评估,直到实际需要它们为止。 If you are able to construct a QuerySet of the exclusion set independently of the larger QuerySet, you can call the .exclude() method and generate one large query instead of two smaller ones. 如果能够独立于较大的QuerySet构造排除集的QuerySet,则可以调用.exclude()方法并生成一个大型查询而不是两个较小的查询。 For example: 例如:

excluded_set = Model.objects.filter(...)
large_set = Model.objects.filter().exclude(id__in=excluded_set)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM