Tuesday, 14 June 2011

Django gets support for IPv6 fields

My favorite web development framework recently committed some code to add support for IPv6 addresses in data models. The commit closes a ticket I opened 6 years ago. Congratulations to Erik Romijn for finally closing this off. Better late than never :)

The new code differs from my original code in a number of ways. First of all it has better testing and documentation. Second, it does IPv6 address validation programmatically rather than using a regular expression.

The code also borrows from ipaddr-py, which I also contribute to.

A variation on my original code is still running and has been used to manage IPv6 addresses so I can vouch for it. Most of my code doesn't actually use IPAddressFields in Django because of this even older issue which was fixed 3 years ago.

Now both of them are fixed I might look at using the new fields.

2 comments:

Daniel said...

Wow... Django's IPv6 validation code is pretty long... I tend to use this instead:

import socket
from django.core.exceptions import ValidationError

def validate_ip_address(value):
  af = socket.AF_INET6 if ':' in value else socket.AF_INET
  try:
    tmp = socket.inet_pton(af, value)
  except socket.error:
    raise ValidationError('Invalid IP address')

matthew said...

@Daniel, socket.AF_INET6 only appeared in Python 2.5 and the validation code they borrowed from ipaddr-py supports 2.4 and later.

That being said, the upcoming release of Django 1.4 will only support Python 2.5 and later so maybe your validation method might work. Have you tried running the tests with your code in place of Django's is_valid_ipv6_address() or ipaddr-py's _BaseV6.is_valid_ip()?