From 6482f1f8877638493810d2740c56e1b62519c8bd Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 23 Feb 2008 09:15:35 +0000 Subject: [PATCH] Fixed #708 -- Fixed searching within IP fields on PostgreSQL. Based on a patch from Matt McClanahan. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7151 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/postgresql/operations.py | 5 +++++ tests/regressiontests/string_lookup/models.py | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py index 20392b238e..2c40b11607 100644 --- a/django/db/backends/postgresql/operations.py +++ b/django/db/backends/postgresql/operations.py @@ -27,6 +27,11 @@ class DatabaseOperations(BaseDatabaseOperations): def deferrable_sql(self): return " DEFERRABLE INITIALLY DEFERRED" + def field_cast_sql(self, db_type): + if db_type == 'inet': + return 'CAST(%s AS TEXT)' + return '%s' + def last_insert_id(self, cursor, table_name, pk_name): cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) return cursor.fetchone()[0] diff --git a/tests/regressiontests/string_lookup/models.py b/tests/regressiontests/string_lookup/models.py index 9deeb18763..1bdb2d4452 100644 --- a/tests/regressiontests/string_lookup/models.py +++ b/tests/regressiontests/string_lookup/models.py @@ -39,6 +39,7 @@ class Base(models.Model): class Article(models.Model): name = models.CharField(max_length=50) text = models.TextField() + submitted_from = models.IPAddressField(blank=True, null=True) def __str__(self): return "Article %s" % self.name @@ -98,4 +99,11 @@ __test__ = {'API_TESTS': ur""" >>> Article.objects.get(text__contains='quick brown fox') + +# Regression test for #708: "like" queries on IP address fields require casting +# to text (on PostgreSQL). +>>> Article(name='IP test', text='The body', submitted_from='192.0.2.100').save() +>>> Article.objects.filter(submitted_from__contains='192.0.2') +[] + """}