0

I want to make a query in python using the equivalent of LIKE in sql.
So for I've been using the __contains option like this :

results = objectName.objects.filter(variable__contains='someword')  

But now I want to put a constraint on the variable like :

filter(variable__contains='_A%')   

with "_" being any character and "%" all character it wants - like in SQL - but it doesn't work :(
Does someone know how to do this ?

1 Answers1

0

Django's queries don't seem to support LIKE out of the box (which is kind of weird).

You can translate your LIKE expression to a regular expression and use __regex:

.filter(variable__regex='.A.*')  

(Another, more advanced way would be to write the LIKE lookup class yourself and register it on CharField.)

AKX
  • 152,115
  • 15
  • 115
  • 172