Skip to content

Instantly share code, notes, and snippets.

@csarcom
Created April 30, 2013 14:16
Show Gist options
  • Save csarcom/5489022 to your computer and use it in GitHub Desktop.
Save csarcom/5489022 to your computer and use it in GitHub Desktop.
class IPField(models.Field):
__metaclass__ = models.SubfieldBase
description = 'IP Field'
def get_db_prep_value(self, value, connection, prepared=False):
value = super(IPField, self
).get_db_prep_value(value, connection, prepared)
if value is not None:
tmp = ipaddr.IPAddress(value)
if ':' in value:
return tmp.packed
else:
return 12*'\0' + tmp.packed
return value
# def get_prep_value(self, value):
# if not value:
# return 'NULL'
# else:
# tmp = ipaddr.IPAddress(value)
# if ':' in value:
# return tmp.packed
# else:
# return 12*'\0' + tmp.packed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment