【问题标题】:Find a string in the entire table (all fields, all columns, all rows) in Django在Django中的整个表(所有字段,所有列,所有行)中查找一个字符串
【发布时间】:2019-11-12 17:47:49
【问题描述】:

我的 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”)出现在不同列的不同行中。 (一个“Both”在“attribute3”列下,另一个“Both”在“Name”列下,最后一个“Both”在“attribute20”列下。

你如何在 Django 中通过查询集得到这个结果?

谢谢

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    假设您已将上表建模为名为 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)
    

    【讨论】:

    • 如果您想从字面上搜索Person 中的所有字段,您可以使用model._meta docs.djangoproject.com/en/2.2/ref/models/meta/… 获取所有字段
    • 当您想要遍历搜索时,它会为您节省很多代码行
    • 我想知道在使用model._meta.get_fields() 访问所有字段作为元组之后如何编写动态过滤器查询。你有例子吗?
    • @rchurch4,nvm 想通了并在回复中添加了代码
    猜你喜欢
    • 2013-10-06
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多