Skip to content

Commit 6482f1f

Browse files
committed
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
1 parent 3240277 commit 6482f1f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

django/db/backends/postgresql/operations.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def date_trunc_sql(self, lookup_type, field_name):
2727
def deferrable_sql(self):
2828
return " DEFERRABLE INITIALLY DEFERRED"
2929

30+
def field_cast_sql(self, db_type):
31+
if db_type == 'inet':
32+
return 'CAST(%s AS TEXT)'
33+
return '%s'
34+
3035
def last_insert_id(self, cursor, table_name, pk_name):
3136
cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
3237
return cursor.fetchone()[0]

tests/regressiontests/string_lookup/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __unicode__(self):
3939
class Article(models.Model):
4040
name = models.CharField(max_length=50)
4141
text = models.TextField()
42+
submitted_from = models.IPAddressField(blank=True, null=True)
4243

4344
def __str__(self):
4445
return "Article %s" % self.name
@@ -98,4 +99,11 @@ def __str__(self):
9899
99100
>>> Article.objects.get(text__contains='quick brown fox')
100101
<Article: Article Test>
102+
103+
# Regression test for #708: "like" queries on IP address fields require casting
104+
# to text (on PostgreSQL).
105+
>>> Article(name='IP test', text='The body', submitted_from='192.0.2.100').save()
106+
>>> Article.objects.filter(submitted_from__contains='192.0.2')
107+
[<Article: Article IP test>]
108+
101109
"""}

0 commit comments

Comments
 (0)