Django:随机排序(order_by(‘?’))进行额外的查询
这是
django中的示例代码.
[情况1] views.py from sampleapp.models import SampleModel from django.core.cache import cache def get_filtered_data(): result = cache.get("result") # make cache if result not exists if not result: result = SampleModel.objects.filter(field_A="foo") cache.set("result",result) return render_to_response('template.html',locals(),context_instance=RequestContext(request)) template.html {% for case in result %} <p>{{ case.field_A}}</p> {% endfor %} 在这种情况下,缓存生成后没有生成查询.我用django_debug_toolbar检查了它. [案例2] views.py – 添加了一行结果= result.order_by(‘?’) from sampleapp.models import SampleModel from django.core.cache import cache def get_filtered_data(): result = cache.get("result") # make cache if result not exists if not result: result = SampleModel.objects.filter(field_A="foo") cache.set("result",result) result = result.order_by('?') return render_to_response('template.html',context_instance=RequestContext(request)) template.html – 与上一个相同 在这种情况下,即使我缓存了已过滤的查询,它也会生成新的查询. 如何在没有额外查询集的情况下调整随机排序? >制作缓存时我无法输入order_by(‘?’). 提前致谢. 解决方法
.order_by在数据库级别执行排序.
这是一个例子.我们在var结果中存储lasy queryset.尚未进行任何查询: results = SampleModel.objects.filter(field_A="foo") 触摸结果,例如,通过迭代它: for r in results: # here query was send to database # ... 现在,如果我们再次这样做,不会尝试数据库,因为我们已经有了这个确切的查询: for r in results: # no query send to database # ... 但是,当您应用.order_by时,查询将会有所不同.所以,django必须向数据库发送新请求: for r in results.order_by('?'): # new query was send to database # ... 解 当您在django中执行查询时,您知道,您将从该查询中获取所有元素(即,没有OFFSET和LIMIT),那么您可以在从数据库获取这些元素之后在python中处理这些元素. results = list(SampleModel.objects.filter(field_A="foo")) # convert here queryset to list 在该行进行了查询,并且您拥有结果中的所有元素. 如果你需要获得随机顺序,现在在python中执行: from random import shuffle shuffle(results) 之后,结果将具有随机顺序,而不会将其他查询发送到数据库. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |