结合Django过滤器

时间:2016-03-26 19:14:04

标签: django django-queryset

我想检查是否存在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

...谢谢

1 个答案:

答案 0 :(得分:0)

通过Q vide:https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects

在过滤器中使用OR语句怎么样?
相关问题