在Django中的整个表格(所有字段,所有列,所有行)中找到一个字符串

我的Django应用程序中有一个模块(表),其中有24个字段(列),我想在其中搜索字符串。我想查看一个列表,向我显示哪些行的字段中包含此字符串。

请查看以下示例:

+-----+------+------+---------+------------+------------+------------+-----+-------------+
| id  | name | year | country | attribute1 | attribute2 | attribute3 | ... | attribute20 | 
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| 1   | Tie  | 1993 | USA     | bond       | Busy       | Busy       | ... | Free        |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| 2   | ness | 1980 | Germany | Free       | Busy       | Both       | ... | Busy        |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| 3   | Both | 1992 | Sweden  | Free       | Free       | Free       | ... | Busy        |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| ... | ...  | ...  | ...     | ...        | ...        | ...        | ... | ...         |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| 24  | Lex  | 2001 | Russia  | Busy       | Free       | Free       | ... | Both        |
+-----+------+------+---------+------------+------------+------------+-----+-------------+

我希望获得的结果(通过使用过滤器等)是这样的:(当我基于整个表和所有记录中的“ Both”一词过滤记录时,每行包含“两者”都在下面的结果中

+----+------+------+---------+------------+------------+------------+-----+-------------+
| id | name | year | country | attribute1 | attribute2 | attribute3 | ... | attribute20 |
+----+------+------+---------+------------+------------+------------+-----+-------------+
| 1  | ness | 1980 | Germany | Free       | Busy       | Both       | ... | Busy        |
+----+------+------+---------+------------+------------+------------+-----+-------------+
| 2  | Both | 1992 | Sweden  | Free       | Free       | Free       | ... | Busy        |
+----+------+------+---------+------------+------------+------------+-----+-------------+
| 3  | Lex  | 2001 | Russia  | Busy       | Free       | Free       | ... | Both        |
+----+------+------+---------+------------+------------+------------+-----+-------------+

您可以看到字符串(“ Both”)出现在不同列的不同行中。 (一个“两个”都在“ attribute3”列下,另一个“两个”都在“名称”列下,最后一个“两个”都在“ attribute20”列下。

如何通过q​​ueryset在Django中获得此结果?

谢谢

xiong_yingwen 回答:在Django中的整个表格(所有字段,所有列,所有行)中找到一个字符串

假设您已将上表建模为名为Person的Django模型

from django.db.models import Q

query_text = "your search string"
Person.objects.filter(
    Q(name__contains=query_text) |
    Q(year__contains=query_text) |
    Q(attribute1__contains=query_text)
and so on for all your attributes
)

上面的代码将进行区分大小写的搜索。相反,如果您希望它不区分大小写,请在上面的代码中使用name__icontains而不是说name__contains

根据@ rchurch4在评论中的建议并基于此so answer,以下是一种可以用更少的代码行搜索整个表的方法:

from functools import reduce
from operators import or_
all_fields = Person._meta.get_fields()
search_fields = [i.name for i in all_fields]
q = reduce(or_,[Q(**{'{}__contains'.format(f): search_text}) for f in search_fields],Q())
Person.objects.filter(q)
本文链接:https://www.f2er.com/3115334.html

大家都在问