Django 模型 Count 和 Q 方法抛出类型错误
Django models Count and Q methods are throwing type error
我正在尝试根据用户输入值生成动态查询。正常的查询是这样的:
result = (
my_model
.objects
.aggregate(
Type1=Count(
'pk',
filter=Q(db_field=1)),
Type2=Count(
'pk',
filter=Q(db_field=2)),
Type3=Count(
'pk',
filter=Q(db_field=3)),
Type4=Count(
'pk',
filter=Q(db_field=4)),
Type5=Count(
'pk',
filter=Q(db_field=5)))
这是我为测试编写的代码:
from django.db.models import (Count, Q)
field = field_values # (it's tuple and read from input)
aggregate_query = ''
if field != None:
for j in field:
aggregate_query += f"{j[1]}={Count}('pk', filter={Q}(db_field={j[0]})),"
my_model.objects.aggregate(aggregate_query[:-1])
当我在 python 控制台上评估时,聚合查询正确生成并得到结果,但是当我执行此代码时,它抛出以下错误:
QuerySet.aggregate() received non-expression(s):
Type1=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=1)),
Type2=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=2)),
Type3=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=3)),
Type4=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=4)),
Type5=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=5)).
有人可以帮我解决这个问题吗?
问题是f"{j[1]}={Count}('pk', filter={Q}(db_field={j[0]})),"
是一个字符串,不能用字符串查询。
你可以构造一个带有推导式的字典,然后将其用于查询:
from django.db.models import (Count, Q)
field = field_values
aggregate_query = {j[1]: Count('pk', filter=Q(db_field=j[0])) for j in field} if j != None else {}
my_model.objects.aggregate(**aggregate_query)
我正在尝试根据用户输入值生成动态查询。正常的查询是这样的:
result = (
my_model
.objects
.aggregate(
Type1=Count(
'pk',
filter=Q(db_field=1)),
Type2=Count(
'pk',
filter=Q(db_field=2)),
Type3=Count(
'pk',
filter=Q(db_field=3)),
Type4=Count(
'pk',
filter=Q(db_field=4)),
Type5=Count(
'pk',
filter=Q(db_field=5)))
这是我为测试编写的代码:
from django.db.models import (Count, Q)
field = field_values # (it's tuple and read from input)
aggregate_query = ''
if field != None:
for j in field:
aggregate_query += f"{j[1]}={Count}('pk', filter={Q}(db_field={j[0]})),"
my_model.objects.aggregate(aggregate_query[:-1])
当我在 python 控制台上评估时,聚合查询正确生成并得到结果,但是当我执行此代码时,它抛出以下错误:
QuerySet.aggregate() received non-expression(s):
Type1=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=1)),
Type2=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=2)),
Type3=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=3)),
Type4=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=4)),
Type5=<class 'django.db.models.aggregates.Count'>('pk', filter=<class 'django.db.models.query_utils.Q'>(db_field=5)).
有人可以帮我解决这个问题吗?
问题是f"{j[1]}={Count}('pk', filter={Q}(db_field={j[0]})),"
是一个字符串,不能用字符串查询。
你可以构造一个带有推导式的字典,然后将其用于查询:
from django.db.models import (Count, Q)
field = field_values
aggregate_query = {j[1]: Count('pk', filter=Q(db_field=j[0])) for j in field} if j != None else {}
my_model.objects.aggregate(**aggregate_query)