gpt4 book ai didi

python - Django Mysql 复杂注解需要别名

转载 作者:行者123 更新时间:2023-11-30 22:19:14 24 4
gpt4 key购买 nike

我正在尝试使用变量作为过滤键值。所有变量都采用我想要的精确值,但随后出现此错误。这是我的观点:

@api_view(['POST'])
def map_data(request):
query_id = request.POST.get('queryId', 1)
group_name = request.POST.get('group', 'district')
query = QueryList.objects.using('teldata').get(query_id=query_id)
groups = query.network_element.lower().split(',')
values = query.kpi_val.lower().split(',')
kwargs = {groups[0]: group_name}

if len(values) == 1:
if len(groups) == 1:
report_data = SiteData.objects.using('teldata').values(groups[0]).annotate(
'latitude', 'longitude', 'latt_call_id', 'long_call_id', Avg(values[0])
).filter(**kwargs)
elif len(groups) == 2:
report_data = SiteData.objects.using('teldata').values(groups[0], groups[1]).annotate(
'latitude', 'longitude', 'latt_call_id', 'long_call_id', Avg(values[0])
).filter(**kwargs)
elif len(values) == 2:
if len(groups) == 1:
report_data = SiteData.objects.using('teldata').values(groups[0]).annotate(
'latitude', 'longitude', 'latt_call_id', 'long_call_id', Avg(values[0]), Avg(values[1])
).filter(**kwargs)
elif len(groups) == 2:
report_data = SiteData.objects.using('teldata').values(groups[0], groups[1]).annotate(
'latitude', 'longitude', 'latt_call_id', 'long_call_id', Avg(values[0]), Avg(values[1])
).filter(**kwargs)
return Response(report_data)

错误日志如下:

Complex annotations require an alias
Request Method: POST
Request URL: http://127.0.0.1:5555/data/map-data/
Django Version: 1.9.5
Exception Type: TypeError
Exception Value:
Complex annotations require an alias
Exception Location: /Library/Python/2.7/site-packages/django/db/models/query.py in annotate, line 895
Python Executable: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.10
Python Path:
['/Users/hco/PycharmProjects/tool/linyit',
'/Applications/PyCharm CE.app/Contents/helpers/pydev',
'/Library/Python/2.7/site-packages/pip-8.1.1-py2.7.egg',
'/Users/hco/PycharmProjects/tool',
'/Applications/PyCharm CE.app/Contents/helpers/pydev',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']

主要是想添加一个可以根据请求参数变化的动态过滤器。如果您建议任何其他方式,我也会打开它。谢谢

最佳答案

这是您的复杂注释。

.annotate(
'latitude', 'longitude', 'latt_call_id', 'long_call_id'
,Avg(values[0]))

此 ORM 查询将转换为包含 group by 子句的 SQL 查询。通常,ORM 将按其中一个键分组并聚合到 values[0] 字段(无论是什么)。但是其他字段 latitudelongitude 等呢?您没有对它们进行任何分组或聚合,这会使查询解析器感到困惑。

如果您花点时间考虑一下这个查询,您会意识到在对特定列进行分组时获取 long_call_id 并没有任何实际意义。 Y事实上老版本的mysql(5.7.6之前)会ch

关于python - Django Mysql 复杂注解需要别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37145144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com