1 2 3 4 5 6 7 8 9 10 11 12 13 | # Will find things like "Art Council", "Artists Ltd", ...
Client.objects.filter(name__istartswith="art")
# ...but won't find "The Art Council", "United Artists", ...
# This will:
Client.objects.filter(name__icontains="art")
# ...but will find things like "Lartistic plc", "Bart Simpson"
# which is not suitable for, say, an autocomplete feature
# Use this instead:
Client.objects.filter(name__iregex=r"(^|'| )art+")
# Will find things like "The Artists", "Art Uni", "Le d'Artis" but not "Bart"
|
More like this
- MultipleEmailsField by spanasik 4 years ago
- create_template_postgis-ubuntu_lucid by clawlor 2 years, 8 months ago
- Simple Admin-button linking to Databrowse by magicrebirth 4 years ago
- simple email validation function by flavio87 4 years, 7 months ago
- whitespaceoptimize block tag by peterbe 4 years, 8 months ago
Comments
I sugget you to use the \b boundary:
Client.objects.filter(name__iregex=r"\bart")
so you can also match "(artist", "{article", ...
#
Using the
\bcan be troubling. Remember the regular expression has to be turned into SQL.In fact I think
\bworks on SQLite but not on PostgreSQL.#