如何编辑查询器的过滤器列表 [英] How to edit filters list of a queryset

查看:150
本文介绍了如何编辑查询器的过滤器列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此功能用于列出搜索结果的视图中。在我的搜索表单中,我有一些ModelChoiceFields通过外键搜索。通常的工作流程意味着使我们当前的搜索越来越精确,所以要禁用很多不相关的结果,我试图删除条目,如果没有其他搜索参数改变,则不会返回结果。

This feature is used in a view where I list search results. In my search form, I have some ModelChoiceFields to search by foreign keys. Usual workflow mean turn our current search more and more precise, so to disable a lot of not pertinent results, I'm trying to remove entries which would return no result if no other search parameters change.

我使用对象查询器来限制某些下拉列表中的命题。
我使用这些下拉列表通过外键我的对象列表过滤。

I'm using an object queryset to restrict propositions in some dropdown lists. I use theses dropdown lists to filter by foreign keys my objects list.

我的过滤器函数参数是一个对象查询集,现在:

My function argument to filter is an Objects queryset, for now :

class MySearchForm(Form):
    things = ModelChoiceField(queryset=models.Thing.objects.none())
    def __init__(self, *args, **kwargs):
        my_objects_queryset = kwargs.pop('my_objects_queryset',models.Thing.objects.all())
        super(MySearchForm, self).__init__(*args, **kwargs)
        self.fields['things'].queryset = \
            models.Thing.objects.filter(object__in=my_objects_queryset).distinct()

我的问题是如何从现有的query_set中删除where close。
这里,我想从my_objects_queryset中删除哪里关闭哪个过滤器 thing = ForeignKey(models.Thing)

My problem is how to remove a 'where close' from an existing query_set. Here, I want to remove from my_objects_queryset where closes which filter by thing = ForeignKey(models.Thing)

可以吗?

有一种方法可以列出我们的查询器的所有过滤器,并即时编辑/删除它们。

Something like a way to list all the filters of our queryset and edit/delete them on the fly.

推荐答案

简短答案:



不,你不能

理论上,这可能是(在某种程度上),但肯定不可取的。

Theoretically, it may be possible (to some extent anyway) but certainly not advisable.

(我还没有完全研究django源码,所以下面是简单爬上调用树的结果。)

查看 QuerySet的来源 filter() exclude()使用添加的新规则返回一个克隆的查询器本身(使用 clone.query.add_q())。

Looking at the source for QuerySet, filter() and exclude() return a clone of the queryset itself with the new rule added on (using clone.query.add_q()).

clone.query 是一个 add_q()方法将新规则添加到 克隆。 query.where ,它本质上是

clone.query is an object representing an SQL Query while the add_q() method adds the new rule to clone.query.where which is essentially a root node of a tree.

新节点如何添加到树中取决于该规则是否与 AND 或一个 OR 子句。这很重要,因为它影响最终SQL查询的正确性,这是生成

How the new node is added to the tree depends on whether the rule is to be connected with an AND or an OR clause. This is important as it affects the correctness of the final SQL query that is generated.

因此,为了列出分配给查询器的过滤器,一个简单的 em>需要了解如何表示 queryset.query.where tree(请参阅 django.utils.tree )。

So, to list filters assigned to a queryset, one "simply" need to understand how the queryset.query.where tree is represented (see django.utils.tree).

当然,删除过滤器也不会影响剩余的规则。我不会提供解决方案,因为不能保证实施不会改变,从而使解决方案无效。我怀疑这是可以做的,但它闻起来只能由学术兴趣完成,或根本就没有。

The difficult bit is of course the removing filters such that it does not affect the remaining rules. I shall refrain from offering a solution as there is no guarantee that the implementation will not change thus invalidating the solution. I suspect it is possible to do but it smells of something that should only be done out of academic interest, or not at all.

这篇关于如何编辑查询器的过滤器列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆