How to edit a query set filter list

This function is used in a view in which I list the search results. In my search form, I have some ModelChoiceFields for searching by foreign keys. A normal workflow means that our current search is becoming more accurate, so in order to disable a lot of inappropriate results, I try to delete records that do not return a result if no other search parameters are changed.

I use a set of query objects to limit the suggestions in some drop-down lists. I use the abstract dropdown lists to filter by foreign key the list of my objects.

My function argument for filtering is the set of object requests at the moment:

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()

My problem is how to remove 'where close' from an existing query_set. Here I want to remove from my_objects_queryset where it closes the filter withthing = ForeignKey(models.Thing)

Is it possible?

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

0
source share
2 answers

Short answer:

No, you cannot do this.

Long answer:

Theoretically, this may be possible (to some extent, in any case), but certainly not practical.

(I have not fully explored the source of django, so the following follows from simply raising the call tree.)

QuerySet, filter() exclude() ( clone.query.add_q()).

clone.query , SQL-, add_q() clone.query.where, node tree.

node , , AND OR. , SQL-, .

, , , , queryset.query.where (. django.utils.tree).

- , , , . , , , , . , , -, .

+6

.

, "" , :

from django.db.models.sql.where import WhereNode

qs = YourModel.objects.filter(column=1).all()
qs.query.where = WhereNode() # resets current filters
qs.all() # now you can apply new filters
+6

Source: https://habr.com/ru/post/1623023/


All Articles