Django Postgresql ArrayField aggregation

﹥>﹥吖頭↗ 提交于 2019-12-01 03:27:29

In PostgreSQL you can do the following:

SELECT DISTINCT UNNEST(array_column) FROM the_table;

So if your model looks something like

class TheModel(models.Model):
    # ...
    array_field = ArrayField(models.CharField(max_length=255, blank=True),\
                             default=list)
    # ...

the Django equivalent is:

TheModel.objects.annotate(arr_els=Func(F('array_field'), function='unnest'))\
                .values_list('arr_els', flat=True).distinct()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!