In the admin interface the search for meta.IPAddressField with a postgresql backend is broken due to a missing cast to text from inet fields:
My model is like this:
class ConnectionType(meta.Model):
name = meta.CharField(maxlength=20)
def __repr__(self):
return self.name
class Connection(meta.Model):
connection_type = meta.ForeignKey(ConnectionType)
source_ip = meta.IPAddressField()
source_port = meta.PositiveIntegerField()
destination_ip = meta.IPAddressField()
destination_port = meta.PositiveIntegerField()
state = meta.CharField(maxlength=20)
def __repr__(self):
return "%s:%d - %s:%d (%s)" % (self.source_ip, self.source_port, self.destination_ip, self.destination_port,
self.state)
class META:
admin = meta.Admin(
list_display = ('connection_type', 'source_ip', 'source_port', 'destination_ip', 'destination_port', 'state'),
list_filter = ['connection_type'],
search_fields = ['source_ip', 'destination_ip'],
)
in the admin inteface the search for an IPv4 address fails with the following traceback:
There's been an error:
Traceback (most recent call last):
File "/usr/lib/python2.3/site-packages/django/core/handlers/base.py", line 71, in get_response
response = callback(request, **param_dict)
File "/usr/lib/python2.3/site-packages/django/contrib/admin/views/decorators.py", line 49, in _checklogin
return view_func(request, *args, **kwargs)
File "/usr/lib/python2.3/site-packages/django/contrib/admin/views/main.py", line 180, in change_list
result_count = p.hits
File "/usr/lib/python2.3/site-packages/django/core/paginator.py", line 67, in _get_hits
self._hits = getattr(self.module, self.count_method)(**order_args)
File "/usr/lib/python2.3/site-packages/django/utils/functional.py", line 3, in _curried
return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))
File "/usr/lib/python2.3/site-packages/django/core/meta/__init__.py", line 1144, in function_get_count
cursor.execute("SELECT COUNT(*)" + sql, params)
File "/usr/lib/python2.3/site-packages/django/core/db/base.py", line 10, in execute
result = self.cursor.execute(sql, params)
ProgrammingError: ERROR: operator does not exist: inet ~~* "unknown"
HINT: No operator matches the given name and argument type(s). You may need to add explicit type casts.
SELECT COUNT(*) FROM firewall_connections WHERE (firewall_connections.source_ip ILIKE '%10.10.1%' OR firewall_connections.destination_ip ILIKE '%10.10.1%')
In the following thread of the post to the pgsql-bugs mailing list is the complete explanation of this "bug":
http://archives.postgresql.org/pgsql-bugs/2003-11/msg00165.php
on this message shows the proper query that should be used:
http://archives.postgresql.org/pgsql-bugs/2003-11/msg00167.php
this is the main reason:
http://archives.postgresql.org/pgsql-bugs/2003-11/msg00170.php