Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按月份名称划分的Django组_Django_Python 3.x - Fatal编程技术网

按月份名称划分的Django组

按月份名称划分的Django组,django,python-3.x,Django,Python 3.x,我想得到一个{'month':'jan','count':1600}的输出,但是我用当前代码{'month':1,'count':1600}得到了以下结果。我如何做到这一点,使我得到的是月份的名称,而不是整数 class Month(Func): function = 'EXTRACT' template = '%(function)s(MONTH from %(expressions)s)' output_field = IntegerField() Note.obj

我想得到一个{'month':'jan','count':1600}的输出,但是我用当前代码{'month':1,'count':1600}得到了以下结果。我如何做到这一点,使我得到的是月份的名称,而不是整数

class Month(Func):
    function = 'EXTRACT'
    template = '%(function)s(MONTH from %(expressions)s)'
    output_field = IntegerField()

Note.objects.filter(
        email_status__in=status_list,
        email_template__isnull=False,
        creation_time__gt=one_year
    ).annotate(
        month=Month('creation_time')
    ).values('month').annotate(
        total_count=Count(
            'email_id', distinct=True)
    ).values('total_count', 'month')
您可以尝试使用而不是
提取
。以下内容未经测试

class Month(Func):
    function = 'EXTRACT'
    template = "%(function)s(%(expressions)s, '%%b')"
    output_field = CharField()

或者,在Python中将整数1-12转换为'jan'-'dec'应该不会太棘手。

您使用的是什么数据库?我使用的是MySQL。