如何过滤查询集中的空名称或 NULL 名称?
我有 first_name
、last_name
和 我需要搜索的alias
(可选)。 因此,我需要一个查询来为我提供所有设置了别名的名称。
除非我能做到:
Name.objects.filter(alias!="")
那么,与上述等效的是什么?
I have first_name
, last_name
& alias
(optional) which I need to search for. So, I need a query to give me all the names that have an alias set.
Only if I could do:
Name.objects.filter(alias!="")
So, what is the equivalent to the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以这样做:
如果您需要排除空值和空字符串,首选方法是将条件链接在一起,如下所示:
将这些方法链接在一起基本上独立地检查每个条件:在在上面的示例中,我们排除了
alias
为 null 或 空字符串的行,因此您将获得所有具有非空值的Name
对象,非空alias
字段。 生成的 SQL 看起来像这样:您还可以将多个参数传递给对
exclude
的单个调用,这将确保只有满足 每个 条件的对象才会被排除:这里,
some_field
和other_field
为 true 的行被排除,因此我们得到两个字段都不为 true 的所有行。 生成的 SQL 代码看起来有点像这样:或者,如果您的逻辑比这更复杂,您可以使用 Django 的 Q 对象:
有关详细信息,请参阅 此页面和此页面 Django 文档。
顺便说一句:我的 SQL 示例只是一个类比——实际生成的 SQL 代码可能看起来会有所不同。 通过实际查看它们生成的 SQL,您将更深入地了解 Django 查询的工作原理。
You could do this:
If you need to exclude null values and empty strings, the preferred way to do so is to chain together the conditions like so:
Chaining these methods together basically checks each condition independently: in the above example, we exclude rows where
alias
is either null or an empty string, so you get allName
objects that have a not-null, not-emptyalias
field. The generated SQL would look something like:You can also pass multiple arguments to a single call to
exclude
, which would ensure that only objects that meet every condition get excluded:Here, rows in which
some_field
andother_field
are true get excluded, so we get all rows where both fields are not true. The generated SQL code would look a little like this:Alternatively, if your logic is more complex than that, you could use Django's Q objects:
For more info see this page and this page in the Django docs.
As an aside: My SQL examples are just an analogy--the actual generated SQL code will probably look different. You'll get a deeper understanding of how Django queries work by actually looking at the SQL they generate.
首先,Django 文档强烈建议不要对基于字符串的字段(例如 CharField 或 TextField)使用 NULL 值。 阅读文档以获取解释:
https://docs.djangoproject。 com/en/stable/ref/models/fields/#null
解决方案:
我认为,您还可以将 QuerySet 上的方法链接在一起。 试试这个:
这应该会给你你正在寻找的套装。
Firstly, the Django docs strongly recommend not using NULL values for string-based fields such as CharField or TextField. Read the documentation for the explanation:
https://docs.djangoproject.com/en/stable/ref/models/fields/#null
Solution:
You can also chain together methods on QuerySets, I think. Try this:
That should give you the set you're looking for.
如果要排除 null (
None
)、空字符串 (""
) 以及包含空格的字符串 (" "
) ,您可以将__regex
与__isnull
过滤选项一起使用alias__isnull=False
排除所有空列aliax__regex = r"\S+ "
确保列值至少包含一个或多个非空白字符。If you want to exclude null (
None
), empty string (""
), as well as a string containing white spaces (" "
), you can use the__regex
along with__isnull
filter optionalias__isnull=False
excludes all the columns null columnsaliax__regex = r"\S+"
makes sure that the column value contains at least one or more non whitespace characters.1. 使用排除时,请记住以下几点以避免常见错误:
不应不将多个条件添加到
排除中()
块类似于filter()
。 要排除多个条件,您应该使用multipleexclude()
。示例:
(NOT a AND NOT b)
等于
================================== ======================
2. 仅当您真正了解时才使用多个:
示例:
NOT (a AND b)
等于
1. When using exclude, keep the following in mind to avoid common mistakes:
Should not add multiple conditions into an
exclude()
block likefilter()
. To exclude multiple conditions, you should use multipleexclude()
.Example:
(NOT a AND NOT b)
equal to
======================================================
2. Only use multiple when you really know about it:
Example:
NOT (a AND b)
equal to
从 Django 1.8 开始,
From Django 1.8,
你可以简单地这样做:
真的就是这么简单。
filter
用于匹配,exclude
用于匹配除指定内容之外的所有内容。 这将在 SQL 中计算为NOT alias='' AND alias IS NOT NULL
。You can simply do this:
It's really just that simple.
filter
is used to match andexclude
is to match everything but what it specifies. This would evaluate into SQL asNOT alias='' AND alias IS NOT NULL
.另一种方法使用通用
isempty
查找,可用于任何字段。它也可以被 django rest_framework 或其他使用 django 查找的应用程序使用:
然后您可以像这样使用它:
Another approach using a generic
isempty
lookup, that can be used with any field.It can also be used by django rest_framework or other apps that use django lookups:
You can then use it like this:
这是另一种简单的方法。
this is another simple way to do it .
这个问题已经很老了,但我会尝试一下,因为我在这里没有看到类似的答案,这仍然可能对将来的人有所帮助。
如果您需要过滤 CharField 或 TextField 或任何其他不应在数据库中存储为 Null 的类似 char 的字段,您可以使用 Q() 对象和否定运算符,如下所示:
这里否定运算符将执行与我们编写的相反的操作在滤波器、反相操作中。
如果您犯了错误并且在基于文本的字段中设置了 null=True,或者您需要能够为空的基于文本的字段存储 Null,您还可以使用 & 连接这两个条件。 (和)或| (或)运算符。 在这种情况下,您将需要 &,类似的东西:
希望有人会发现这很有用。
This question is quite old, but i will give it a shot since i see no similar answer here and this still might help somebody in future.
If you need to filter CharField or TextField or any other char-like field that should not be stored as Null in the database, you can use Q() object and negating operator, like this:
Here negating operator will do opposite of what we write in filter, inverting operation.
In case you've made a mistake and have null=True at your text-based field, or you need to be able to store Null for empty text-based field, you can also join those two conditions using & (and) or | (or) operators. In this case you will need &, something along those lines:
Hope somebody will find this useful.