I am writing a Django application that stores IP addresses with additional routing information. One of the fields for the IP model that I created is nexthop(for next hop routes), which will usually be empty. We originally intended to use MySQL, but now the project requirements have changed to use PostgreSQL.
Here is a stripped down version of my model:
class IP(models.Model):
address = models.IPAddressField()
netmask = models.IPAddressField(default='255.255.255.255')
nexthop = models.IPAddressField(null=True, blank=True, default=None)
active = models.BooleanField('is active?', default=1)
So, with MySQL, I had no problem leaving the field nexthopempty. However, now that I have switched the development environment to Postgres, we are faced with a known problem in Django 1.1.1 in which an empty IP address causes aDataError
invalid input syntax for type inet: ""
LINE 1: ...-14 13:07:29', 1, E'1.2.3.4', E'255.255.255.255', E'', true)
^
, , , NULL.
, IP- , .
Django , , next-hop 255.255.255.255 - (.. next-hop 255.255.255.255, ), .
, - , Django , , .
!
:
( ) :
:
IP_NEXTHOP_SENTINEL = '255.255.255.255'
class IP(models.Model):
nexthop = models.IPAddressField(
null=True,
blank=True,
default=IP_NEXTHOP_SENTINEL,
help_text='Use %s to indicate no next-hop' % IP_NEXTHOP_SENTINEL
)
def save(self, *args, **kwargs):
if self.nexthop and self.nexthop == IP_NEXTHOP_SENTINEL:
self.nexthop = None
:
IP , null=True nexthop. , 255.255.255.255 - , , , , save(), None, , , .
!