[Answer]-Regex search from django.db.connection?

1👍

I think, you need this:

from django.db import connection
cursor = connection.cursor()
code = 'statin'
query = 'SELECT * FROM chemical WHERE name ~* {}'
print query.format(code)
cursor.execute(query.format(code))
print cursor.fetchall()

There’s nothing to do with Django, you have to format the string properly.

0👍

Actually, the Django ORM can perform regex lookups for you, using the __regex lookup, as seen here: https://docs.djangoproject.com/en/1.8/ref/models/querysets/#regex

Or, as you are doing there, a case insensitive regex, with the __iregex lookup: https://docs.djangoproject.com/en/1.8/ref/models/querysets/#iregex

Leave a comment