如何从Django模型中选择多个列?

时间:2022-10-04 19:23:28

At the moment I have a query that selects distinct values from a model:

目前我有一个查询从模型中选择不同的值:

Meeting.objects.values('club').distinct()

In addition to the 'club' field, I also wish to select a 'time' field. In other words I wish to select distinct values of the 'club' field and the associated 'time' field. For example for:

除了'俱乐部'领域,我还希望选择一个'时间'字段。换句话说,我希望选择“俱乐部”字段和相关“时间”字段的不同值。例如:

CLUB,TIME

ABC1,10:35
ABC2,10:45
ABC2,10:51
ABC3,11:42

I would want:

我想要:

ABC1,10:35
ABC2,10:45
ABC3,11:42

What is the syntax for this?

这是什么语法?

1 个解决方案

#1


1  

This is possible, but only if your database backend is PostgreSQL. Here how it can be done:

这是可能的,但前提是您的数据库后端是PostgreSQL。在这里如何做到:

Meeting.objects.order_by('club', 'time').values('club', 'time').distinct('club')

Look documentation for distinct

查看文档以区别

#1


1  

This is possible, but only if your database backend is PostgreSQL. Here how it can be done:

这是可能的,但前提是您的数据库后端是PostgreSQL。在这里如何做到:

Meeting.objects.order_by('club', 'time').values('club', 'time').distinct('club')

Look documentation for distinct

查看文档以区别