在django模型上实现通配符搜索

时间:2018-05-07 15:37:01

标签: python django

我有一个名为Host的模型,我存储了有关所有服务器的所有信息。在模型中,我有一个字段{ "name": "rn2-test2", "version": "0.1.0", "private": true, "devDependencies": { "react-native-scripts": "1.14.0", "jest-expo": "~27.0.0", "react-test-renderer": "16.3.1" }, "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js", "scripts": { "start": "react-native-scripts start", "eject": "react-native-scripts eject", "android": "react-native-scripts android", "ios": "react-native-scripts ios", "test": "jest" }, "jest": { "preset": "jest-expo" }, "dependencies": { "expo": "^27.0.1", "react": "16.3.1", "react-native": "~0.55.2", "react-navigation": "^1.5.12" } }

我希望用户能够指定通配符搜索,例如:hostname,他们将获得主机名以*one结尾的所有主机或 one他们将获得所有主机名为one*的主机 和one以及one*two*one*two以及*one*two**one*two*three等等。

我试过迭代one*two*three的结果但是如果/ elif继续混乱我最终会感到困惑。有没有人知道更优雅的方式处理通配符?

1 个答案:

答案 0 :(得分:1)

您在寻找fnmatch吗?

import fnmatch

print(fnmatch.fnmatch('foo.info', 'foo*'))
print(fnmatch.fnmatch('foo.info', '*.info'))
print(fnmatch.fnmatch('nope', '*.info'))

给出

True
True
False

你仍然需要迭代,但必须处理一个if