Model IPAddressField not validating?

821 views
Skip to first unread message

candlerb

unread,
Jul 5, 2011, 12:14:40 PM7/5/11
to Django users
I have a model defined as follows with an IPAddressField:

~~~~
class Nas(models.Model):
name = models.CharField(max_length=128)
ip = models.IPAddressField('IP Address', max_length=15,
unique=True)
nas_type = models.CharField('NAS Type', max_length=32, blank=True)
huntgroup = models.ForeignKey(Huntgroup)

class Meta:
verbose_name = "NAS"
verbose_name_plural = "NASes"

def __unicode__(self):
return self.name + " (" + self.ip + ")"
~~~~

However, the model doesn't seem to validate that the ip field is a
valid IP address. Example:

>>> n = Nas(ip='wibble', name='bibble', huntgroup_id=12)
>>> n.full_clean()
>>> n.ip
'wibble'
>>>

However, it does validate other things, e.g. the presence of required
fields.

>>> n = Nas(ip='wibble', huntgroup_id=12)
>>> n.full_clean()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/
django/db/models/base.py", line 828, in full_clean
raise ValidationError(errors)
ValidationError: {'name': [u'This field cannot be blank.']}

is this a bug, or am I using it wrongly? I want to create models in
code, not in a form, but validate them before saving.

Thanks,

Brian.

Tom Evans

unread,
Jul 5, 2011, 12:34:10 PM7/5/11
to django...@googlegroups.com
On Tue, Jul 5, 2011 at 5:14 PM, candlerb <b.ca...@pobox.com> wrote:
> I have a model defined as follows with an IPAddressField:
>
> ~~~~
> class Nas(models.Model):
>    name = models.CharField(max_length=128)
>    ip = models.IPAddressField('IP Address', max_length=15,
> unique=True)
>    nas_type = models.CharField('NAS Type', max_length=32, blank=True)
>    huntgroup = models.ForeignKey(Huntgroup)
>
>    class Meta:
>        verbose_name = "NAS"
>        verbose_name_plural = "NASes"
>
>    def __unicode__(self):
>        return self.name + " (" + self.ip + ")"
> ~~~~
>
> However, the model doesn't seem to validate that the ip field is a
> valid IP address. Example:
>

No, it doesn't have any model level validation. If you check the
source, it is simply a char field with max length of 15 characters.
It's form field does have validation though, and you can add the same
validator used there as a validator on the model field:

from django.core.validators import validate_ipv4_address
...
ip = models.IPAddressField(..., validators=[validate_ipv4_address])

Also, as you can see in the name, this only supports IPv4. For a more
complete solution (requires postgres) I use the excellent
django-postgresql-netfields -
https://github.com/adamcik/django-postgresql-netfields

Cheers

Tom

Tim Shaffer

unread,
Jul 5, 2011, 1:37:56 PM7/5/11
to django...@googlegroups.com
Django trunk also contains validate_ipv6_address and validate_ipv46_address.

But as of 1.3 I think it only validates ipv4

candlerb

unread,
Jul 5, 2011, 2:09:02 PM7/5/11
to Django users
On Jul 5, 5:34 pm, Tom Evans <tevans...@googlemail.com> wrote:
> from django.core.validators import validate_ipv4_address
> ...
>     ip = models.IPAddressField(..., validators=[validate_ipv4_address])

This works perfectly, thank you. (Maybe worth a note in ref/models/
fields.html though?)

Cheers,

Brian.
Reply all
Reply to author
Forward
0 new messages