As the title mentions, I am working in Django and trying to make a QuerySet to return all "Customer" models that have a name value that is a substring of my query_string.
I want something like this:
Customer.objects.filter(firstName__icontains=query_string)
But in reverse:
Customer.objects.filter(query_string__icontains=firstName)
Except that obviously doesn't work.
I am struggling to find any information on how I would go about this.
Thanks in advance.
Edit My expected input will be in Japanese - Kana and Kanji かな、カナ、漢字
Thanks to everyone for the answers! I was able to solve my problem by building off of @brad's answer.
I took his approach of defining a custom lookup. It pretty much looks like his, but I configured it for MySQL.
class LowerContainedBy(Lookup):
lookup_name = 'icontained_by'
def as_mysql(self, compiler, connection):
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs, rhs_params = self.process_rhs(compiler, connection)
params = lhs_params + rhs_params
return "LOWER({0}) LIKE LOWER(CONCAT('%%',{1},'%%'))".format(lhs, rhs), params
Field.register_lookup(LowerContainedBy)
(I also noticed the rhs and lhs was flipped in his answer so keep that in mind!)
And finally by implementing the new lookup like so:
searchResults = Customer.objects.filter(firstName__icontained_by=search_string).exclude(contract='').order_by('-dateCreated')