django 值列表只获取一次值

django values list get values only once

我想要一个列表,其中包含仅出现一次的查询集中的值。

我看过 this post 但这似乎对我不起作用。

示例:

CharacterJournal.objects.order_by("date").values_list("reftypeid", flat=True).distinct()

returns 像这样:

[97, 96, 97, 97, 97, 97, 97, 97, 42, 42, 42,  52, 42, 42, 1, 42, '...(remaining elements truncated)...']

请大家帮忙

请参阅 distinct [1] 文档中的说明:

Any fields used in an order_by() call are included in the SQL SELECT columns. This can sometimes lead to unexpected results when used in conjunction with distinct(). If you order by fields from a related model, those fields will be added to the selected columns and they may make otherwise duplicate rows appear to be distinct. Since the extra columns don’t appear in the returned results (they are only there to support ordering), it sometimes looks like non-distinct results are being returned.

Similarly, if you use a values() query to restrict the columns selected, the columns used in any order_by() (or default model ordering) will still be involved and may affect uniqueness of the results.

换句话说,因为您按 date 排序,查询 returns 对 (date, reftypeid)DISTINCT 子句是在这一对上执行的。 date 后来因为 values_list 而被删除,但是没有额外删除重复的 reftypeid 条目。