gpt4 book ai didi

python - 通过自定义 Func 的 Django ORM 组

转载 作者:行者123 更新时间:2023-11-28 18:56:27 30 4
gpt4 key购买 nike

我正在使用 Django,我需要通过自定义函数对模型进行分组。

好的,我想要的查询是:

SELECT date_trunc('week',"model_table"."date") AS "date" FROM "model_table" GROUP BY "date"

然后我试过的是:

class DateTrunc(Aggregate):
function = 'date_trunc'
template = "%(function)s('%(date_type)s',%(expressions)s)"
date_types = ['microsecond', 'millisecond', 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year', 'decade', 'century', 'millenium']

def __init__(self, date_type, expression, group_by=False, **extra):
self.must_group_by = group_by # Name "group_by" is already used.
if date_type not in self.date_types:
raise AttributeError('The specified "date_type" is not correct.')
super().__init__(expression, date_type=date_type, **extra)

def get_group_by_cols(self):
if self.must_group_by:
return [self]
return super().get_group_by_cols()

In [1]: Model.objects.annotate(date=DateTrunc('week','date',True)).values('date').query.__str__()
Out[1]: 'SELECT date_trunc(\'week\',"model_table"."date") AS "date" FROM "model_table" GROUP BY "model_table"."id", date_trunc(\'week\',"model_table"."date")'

除了 GROUP BY 子句还插入了表的唯一 ID 之外,它还能找到。这意味着最终结果根本没有分组。

我一直在检查 Django 代码,在 Query 类中有一个函数 set_group_by 就是那个把它搞得一团糟的函数,但我真的不知道怎么做破解它。

无论如何我认为正确的方法是继承 Func 类而不是 Aggregate 但是我无法让 Djangot 插入 GROUP最终 SQL 中的 BY 子句。

知道如何在不使用 cursorRawSQL 或任何其他直接编写查询的方式对查询进行硬编码的情况下解决这个问题吗?

最佳答案

好吧,我找到了一种使用 Func 的方法,尽管它只是一个补丁。因此,首先制作自定义函数:

class DateTrunc(Func):
function = 'date_trunc'
template = "%(function)s('%(date_type)s',%(expressions)s)"
date_types = ['microsecond', 'millisecond', 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year', 'decade', 'century', 'millenium']

def __init__(self, date_type, expression, **extra):
if date_type not in self.date_types:
raise AttributeError('The specified "date_type" is not correct.')
super().__init__(expression, date_type=date_type, **extra)

然后我们有一个简单的自定义函数,我们的查询可以是:

In [7]: Model.objects.annotate(date=DateTrunc('week','date')).values('date').annotate(Avg('parameter')).query.__str__()                                       
Out[7]: 'SELECT date_trunc(\'week\',"model_table"."date") AS "date", AVG("model_table"."parameter") AS "parameter__avg" FROM "model_table" GROUP BY date_trunc(\'week\',"model_table"."date")'

在这个解决方案中,他的parameter 或聚合函数Avg 可以是任意的,不会被使用。只是根据我的需要按日期对 Django 进行分组的修复。

关于python - 通过自定义 Func 的 Django ORM 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57823059/

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