0

虽然 my_sql 或 mariaDB 上 Group_Concat 的 Django 实现似乎有据可查,但我无法使用 SQLITE 上的可选分隔符使 Group_Concat 在 Django 中工作。

class GroupConcat(Aggregate):
function = 'GROUP_CONCAT'
    separator = '-'

    def __init__(self, expression, separator='-', distinct=False, ordering=None, **extra):
        super(GroupConcat, self).__init__(expression,
                                          distinct='DISTINCT ' if distinct else '',
                                          ordering=' ORDER BY %s' % ordering if ordering is not None else '',
                                          output_field=CharField(),
                                          **extra)
        self.separator = separator

使用 distinct 选项时,它与默认分隔符 ',' 配合得很好

def as_sqlite(self, compiler, connection, **extra_context):
        if self.separator:
            return super().as_sql(
                compiler, connection, separator=self.separator,
                template="%(function)s(%(distinct)s%(expressions)s)",
                **extra_context
            )

我发现使用 SQLITE,我们不能同时使用 distinct 和 declare 分隔符,但我不明白为什么没有 distinct 就不能工作,如下所示:

def as_sqlite(self, compiler, connection, **extra_context):
    
            return super().as_sql(
                compiler, connection, separator=self.separator,
                template="%(function)s(%(expressions)s,%(separator)s)",
                **extra_context
            )

无论我尝试什么,我总是以错误告终:

File "C:\Users\Gil\Gilles\Perso\Maison\Projets perso\Formacube\Formacube test App\Github_FormApp\FormApp\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: near ")": syntax error
4

0 回答 0