【发布时间】:2013-06-04 05:38:11
【问题描述】:
我可以看到很多不同的选项来执行此操作,并希望获得有关最有效或“最佳实践”方法的一些反馈。
我得到一个带有 filter() 的 Django 查询集
c_layer_points = models.layer_points.objects.filter(location_id=c_location.pk,season_id=c_season.pk,line_path_id=c_line_path.pk,radar_id=c_radar.pk,layer_id__in=c_layer_pks,gps_time__gte=start_gps,gps_time__lte=stop_gps)
这个查询集可能非常大(数十万行)。
现在需要进行的是转换为列表和编码为 JSON。
选项(我在搜索中看到的):
- 遍历查询集
例子:
gps_time = [lp.gps_time for lp in c_layer_points];
twtt = [lp.twtt for lp in c_layer_points];
- 使用 values() 或 values_list()
- 使用迭代器()
最后,我想将类似以下格式的 json 编码为:
{'gps_time':[list of all gps times],'twtt',[list of all twtt]}
任何关于最佳方式的提示都会很棒,谢谢!
【问题讨论】:
-
您为什么不自己进行测量?
import datetime; start = datetime.time(); <do your stuff> end = datetime.time(); print(end-start) -
我已经并将继续这样做。但是,我想从其他人那里了解一下这里的“最佳实践”。特别是因为从效率的角度来看,其中一些选项几乎相同。
标签: python django list filter django-queryset