Created
October 5, 2012 16:13
-
-
Save Apkawa/3840773 to your computer and use it in GitHub Desktop.
IPATONField
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import struct | |
class IPATONField(models.IntegerField): | |
''' | |
Для прозрачной обратной совместимости с данными, которые записывались при помощи INET_ATON | |
''' | |
__metaclass__ = models.SubfieldBase | |
def to_python(self, value): | |
if isinstance(value, basestring): | |
return value | |
packed_value = struct.pack('!I', value) | |
return socket.inet_ntoa(packed_value) | |
def get_prep_value(self, value): | |
"""Convert IP for save""" | |
packed = socket.inet_aton(value) | |
unpacked_value = struct.unpack('!I', packed)[0] | |
return super(IPATONField, self).get_prep_value(unpacked_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment