【发布时间】: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