1π
β
Why are you storing usernames in your database with leading and trailing spaces? Trim them before you insert them instead of allowing dirty data in your database.
But to answer your question directly, you donβt need to use extra
to do a regular expression lookup, as the queryset API natively supports it.
This should do what you want:
User.objects.filter(username__regex=r"\s*" + user + r"\s*")
Things get a little messy on SQLLite, where regex lookups arenβt natively supported, but it still works.
π€Brett Gmoser
1π
extra can be used. This is what it would look like for PostgreSQL:
User.objects.extra(where=["username != regexp_replace(username, '^\s+|\s+$', '', 'g')"])
π€Adrian Ghiuta
0π
from django.db.models import Q
User.objects.filter(Q(username__startswith=' ') | Q(username__endswith=' '))
π€madzohan
- [Answered ]-Checkbox not working in a Django form (bug?)
- [Answered ]-Django method call from template sets named variable. How is this even possible?
- [Answered ]-Django url patterns, how to match two different patterns with multiple options?
Source:stackexchange.com