# One step up from __icontains

Author:
peterbe
Posted:
September 8, 2010
Language:
Python
Version:
1.2
Tags:
ORM QuerySet
Score:
1 (after 1 ratings)

The REGEX and IREGEX operators were added in Django 1.0 and I'm sure you can think of fancier ways of doing word delimiting and things like that but this was all I needed to make a user-friendly autocomplete search function.

 ``` 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

1. Annotate queryset with comment count by fivethreeo 7 years, 11 months ago
2. get_queryset_or_404 by mattpdx 8 years, 3 months ago
3. Changing queryset of a ModelChoiceField by mimog 5 years, 1 month ago
4. Custom Model Manager Chaining by hunterford 6 years, 1 month ago
5. Check condition in Form Queryset by felipecruz 4 years, 8 months ago

<p>Using the `\b` can be troubling. Remember the regular expression has to be turned into SQL. </p> <p>In fact I think `\b` works on SQLite but not on PostgreSQL. </p>