diff --git a/AUTHORS b/AUTHORS index 5fcdf833f0..ba1f4036e9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -152,6 +152,7 @@ answer newbie questions, and generally made Django that much better: Antonis Christofides Michal Chruszcz Can Burak Çilingir + Andrew Clark Ian Clelland Travis Cline Russell Cloran @@ -273,6 +274,7 @@ answer newbie questions, and generally made Django that much better: Brian Harring Brant Harris Ronny Haryanto + Axel Haustant Hawkeye Kent Hauser Joe Heck diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py index f96757da8b..c5aab84693 100644 --- a/django/db/backends/postgresql_psycopg2/operations.py +++ b/django/db/backends/postgresql_psycopg2/operations.py @@ -69,7 +69,7 @@ class DatabaseOperations(BaseDatabaseOperations): # Cast text lookups to text to allow things like filter(x__contains=4) if lookup_type in ('iexact', 'contains', 'icontains', 'startswith', - 'istartswith', 'endswith', 'iendswith'): + 'istartswith', 'endswith', 'iendswith', 'regex', 'iregex'): lookup = "%s::text" # Use UPPER(x) for case-insensitive lookups; it's faster. diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 18c4029649..b9caf8b99f 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -522,4 +522,4 @@ def _sqlite_format_dtdelta(dt, conn, days, secs, usecs): return str(dt) def _sqlite_regexp(re_pattern, re_string): - return bool(re.search(re_pattern, re_string)) + return bool(re.search(re_pattern, str(re_string))) if re_string is not None else False diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py index de7105f92d..fe8a5fac64 100644 --- a/tests/lookup/tests.py +++ b/tests/lookup/tests.py @@ -610,6 +610,21 @@ class LookupTests(TestCase): self.assertQuerysetEqual(Article.objects.filter(headline__regex=r'b(.).*b\1'), ['', '', '']) + def test_regex_null(self): + """ + Ensure that a regex lookup does not fail on null/None values + """ + Season.objects.create(year=2012, gt=None) + self.assertQuerysetEqual(Season.objects.filter(gt__regex=r'^$'), []) + + def test_regex_non_string(self): + """ + Ensure that a regex lookup does not fail on non-string fields + """ + Season.objects.create(year=2013, gt=444) + self.assertQuerysetEqual(Season.objects.filter(gt__regex=r'^444$'), + ['']) + def test_nonfield_lookups(self): """ Ensure that a lookup query containing non-fields raises the proper