Reputation: 113
I am new to Django framework.
I am using :
python==2.7_win32
Django==1.11.3
djangorestframework==3.6.3
I am creating an API which do not involve any database.
Flow of data is :
User Request in JSON
Invoking method in Django's views.py
This method calls my local package that is not part of django and gets result
Return this result in the Response
Problem is : It works perfectly in the first request but on the next request it gives previous result only.
I found that django caches the Queryset
and we can force to read new data by adding all()
method.
But my code do not involve any Database or Model. So I think QuerySet will also not created.But still my results are getting cached somehow. how can I stop it?
Here is my view :
@api_view(['POST'])
@never_cache
def filter_details(request):
data = {}
data['data'] = {}
result_array = []
error = None
try:
attr1 = request.data.get("attr1")
attr2 = request.data.get("attr2")
local_package_obj = local_package.ClassName(attr1)
result_array = local_package_obj.get_some_data_as_array(attr2)
except Exception as e:
error = str(e)
data['data']['important'] = result_array
data['error'] = error
return Response(data)
Even after applying different values for att1 and attr2 I am getting same results. I have checked local package. I works properly. The problem is in caching only.
Upvotes: 1
Views: 1496
Reputation: 20966
I works properly. The problem is in caching only.
No it doesn't work properly. If you face some caching issues, you probably have a scope issue somewhere and you're not aware of it.
For example, if you have something like this in your module:
# This is a file
queryset = SomeModel.objects.all()
def my_function():
return list(queryset) # list() will force the QS evaluation
If you call my_function()
it'll will return a list of instances. Add or remove some instances and you won't see a change.
The reason is queryset
is evaluated once and will stay evaluated.
if you change the code as:
def my_function():
return list(queryset.all())
you'll see the list change. This is because .all()
will return a new queryset which means it is not evaluated.
Most of the time, I prefer to be explicit and create the queryset directly from the function:
def my_function():
return list(SomeModel.objects.all())
Upvotes: 4