我可以看到很多不同的选项来执行此操作,并希望获得有关最有效或“最佳实践”方法的一些反馈。
我得到一个带有 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]}
任何有关执行此操作的最佳方法的提示都会很棒,谢谢!