我使用distinct()
QuerySet在Django中獲取一些數據。
我最初的詢問是Point.objects.order_by('chron', 'pubdate')
。
字段chron
在某些情況下是重複的,因此我將查詢 更改爲Point.objects.order_by('chron', 'pubdate').distinct('chron')
以排除重複項。 現在的問題是,所有空字段都被認爲是重複的。查詢排除Django中的重複
爲了準確,chron
字段包含整數(其行爲與id類似),在某些情況下,它可以是重複的,在某些情況下,它可以是NULL。
| chron |
|-------|
| 1 | I want this
| 2 | I want this
| 3 | I want this
| 3 |
| NULL |
| 4 | I want this
| NULL |
我要排除所有的chron
副本但如果它們是重複的NULL的。 謝謝。
檢查此。 https://stackoverflow.com/questions/30084107/django-query-with-order-by-distinct-and-limit-on-postgresql。然後排除以過濾空結果。 – Windsooon