I'm working on an app that requires me to filter through large amount of records. I've been reading about caching QuerySets and related stuff and found some good material. For example:
Caching query results in django https://docs.djangoproject.com/en/dev/topics/db/queries/#caching-and-querysets
also something about QuerySet working etc. https://docs.djangoproject.com/en/dev/topics/db/optimization/#understand-queryset-evaluation https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.iterator
There are some things still unclear though.
In my app it is somewhat like this: [I do have validations etc, not written here]
qs = MyModel.objects.filter(Q( <initial_filter_to_narrow_down_size> ))
#I let user to specify other filters and I use qs further
q_object = Q(< [using user_made_filters].pop()> )
for filter in user_made_filters:
q_object |= Q( <using filter> )
qs = qs.filter(q_object)
there can be n number of user_made_filters. For some of them I need to do &= instead of |=.
Questions:- 1]
qs = MyModel.objects.filter(Q(<initial_filter_to_narrow_down_size>))
After this, I wish to put this qs in cache for later use. I want to apply all the other filters without hitting the database. something like
cache.set('qs', qs)
but what happens when I will do qs = qs.filter(q_object) ? Cache will be modified ? I don't want that. I want qs to remain constant until I update it. What should I do in this case ?
2] Like I said before, I generate OR'ed or ANDed Q objects and I keep doing qs = qs.filter(q_object). I do this because I find similar type of filters at once, apply them then get another type of filters and go on. I also need to prioritize some filters. They will be applied first and then rest of them. is there any difference between: asking this because in my idea my qs is of type (a).
(a) MyModel.objects.filter( Q(condition1) ).filter( Q(condition2) ) and
(b) MyModel.objects.filter( Q(condition1) & Q(condition2) ) ?
I mean when qs is finally evaluated, will django evaluate as per (a) ? or will it combine all filters and evaluate it like (b) ?
Regards,
ps- won't be able to reply back for 2-3 hours.