Django has a marvalous database access optimization for querysets. Whenever same queries are tried to be executed, django does not access the database repeadetly. It is using cache for same parts. This is an effective optimization for database queries. But when a query has frequent changes , this optimization might be problem.
Here is my query needs to be executed on every time;
expires = Expire.objects.filter(created_date__lte=(datetime.now() - F("duration") * F("percent") / 100.0))
Query is simply to find expires which its duration is exceeded its percent. For example; if duration is 10 sec and percent is 80; this query will find expires which is created at least 8 seconds ago. It doesn't matter anyway. The point is, my queryset result is changing frequently, every second. When query is executed, its result is not changed even after 8 seconds. I tried to find disabling this caching optimization at least for my queryset. But there is no direct way to do it. There are other ways which makes queryset is evaluated. They are listed on Django Queryset Documentation. I used one of them which is
exists() function of queryset.
repr() can also be used.
expires = Expire.objects.filter(created_date__lte=(datetime.now() - F("duration") * F("percent") / 100.0)) # to reset cache and re-evaluate queryset expires.exists() #or repr(expires) #or len(expires) #Now expires queryset is re-evaluated.