我想检查是否存在chron_id
并获取没有重复的行。
如果chron_id
为null,则获取所有行。
toponymies_one = Toponymy.objects.filter(chron_id__isnull=False).distinct('chron_id')
toponymies_two = Toponymy.objects.filter(chron_id__isnull=True)
我应该合并这两个查询集吗?有没有更好的方法呢?
| id | somevalue | somevalue | chron_id |
|----|-----------|-----------|----------|
| 1 | foo | foo | | # I want this row
| 2 | bar | foo | 2 |
| 3 | foo | bar | | # I want this row
| 4 | foo | foo | | # I want this row
| 5 | bar | foo | 5 |
| 6 | foo | bar | | # I want this row
| 7 | foo | foo | | # I want this row
| 8 | bar | foo | 5 | # I want this row
| 9 | foo | bar | 2 | # I want this row
...谢谢
答案 0 :(得分:0)
通过Q
vide:https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects