在djnago中过滤queryset,其中一个字段具有特定值或为null

发布于 2025-02-05 05:43:07 字数 141 浏览 1 评论 0原文

在django中做到这一点的正确方法是什么,我想过滤一个字段具有特定值或为null的QuerySet,这意味着,如果我按名为“类型”的字段过滤QuerySet,它将返回带有对象的QuerySet具有类型的特定值,例如“ Active”,并且具有“类型”的对象具有空值。

what's the right way to do this in Django, i want to to filter a queryset where the field has a specific value or is null,which means that if i filter a queryset by field named "type", it would return a queryset with objects has a specific value of type like "active" and also object with "type" has null value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤檠 2025-02-12 05:43:08

您可以与 q 对象  [django-doc] 为此:

MyModel.objects.filter(
    Q(type='active') | Q(type=None)
)

在这里,我们将两个q对象组合在一起,它们充当过滤器使用位或操作员|,这意味着我们可以创建一个用作逻辑或两个过滤器条件之间的过滤器。

You can work with Q objects [Django-doc] for this:

MyModel.objects.filter(
    Q(type='active') | Q(type=None)
)

Here we combine the two Q objects, which act as filters with a bitwise or operator |, and this means that we create a filter that acts as a logical or between the two filter conditions.

吹梦到西洲 2025-02-12 05:43:08

DJANGO文档中有一个部分与模型现场查找。但是,如果您不想弄乱它,这里有一些例子:

# Obtaining all non-null fields
Model.objects.filter(some_field__isnull=False)

# Obtaining all rows with field value greater than some value
Model.objects.filter(some_field__isnull=False, some_field__gt=value)

# Actually the better way of writing complex queries is using Q objects
from django.db.models import Q
Model.objects.filter(Q(some_field__isnull=False) & Q(some_field__gt=value)) # The same thing as the previous example

# Using Q objects in this situation may seem useless and it kinda is, but as for me
# This improves readability of your query

您可能想处理更多的事情,但我认为我描述了最常见的用例。
编辑
正如Willem答案中所写的,以获取具有“活动”或null值的行,您应该编写此类查询:

Model.objects.filter(Q(type='active') | Q(type__isnull=True))

There is a whole section in Django documentation related to model field lookups. But if you don't want to mess with it here is some examples:

# Obtaining all non-null fields
Model.objects.filter(some_field__isnull=False)

# Obtaining all rows with field value greater than some value
Model.objects.filter(some_field__isnull=False, some_field__gt=value)

# Actually the better way of writing complex queries is using Q objects
from django.db.models import Q
Model.objects.filter(Q(some_field__isnull=False) & Q(some_field__gt=value)) # The same thing as the previous example

# Using Q objects in this situation may seem useless and it kinda is, but as for me
# This improves readability of your query

There are a lot more things that you may want to do with your field, but I think that I described the most common use case.
Edit
As written in the Willem's answer to obtain rows that has value of "active" or null you should write such query:

Model.objects.filter(Q(type='active') | Q(type__isnull=True))
并安 2025-02-12 05:43:08

我认为这是实现您目标的最简单技术。

model.objects.filter(q(type ='active')| q(type = true))

I think this is the simplest technique to achieve your aim.

Model.objects.filter(Q(type='active') | Q(type=True))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文