Missing/inconsistent GenericIPAddressField validation?

144 views
Skip to first unread message

Hans S. Tømmerholt

unread,
Sep 7, 2015, 6:42:04 AM9/7/15
to django...@googlegroups.com

Hello.

In a nutshell:
It appears the validator applied to a GenericIPAddressField form field
doesn't allow an IP address with a netmask.
But saving the same thing to a PostgreSQL database via a model works just
fine.

As far as I can tell, both model and form fields use the same validators?
Is this somehow expected?


class IPAddressList(models.Model):
address = models.GenericIPAddressField()


class IPAddressListForm(ModelForm):
class Meta:
model = IPAddressList
exclude = []


ip_address = "127.0.0.1/8"
o = IPAddressList.objects.create(address=ip_address)
print o.address
# 127.0.0.1/8

But then:

form = IPAddressListForm({"address": ip_address})
form.is_valid()
print form.errors
# <ul class="errorlist"><li>address<ul class="errorlist"><li>Enter a valid
IPv4 or IPv6 address.</li></ul></li></ul>

I'm using Django 1.8.

I keep looking for stupid mistakes I might have made. Suggestions welcome.

--
Best regards/vennlig hilsen
Hans S. Tømmerholt
Application Developer, Hosting, Global IT
Opera Software

Hans S. Tømmerholt

unread,
Sep 7, 2015, 7:33:52 AM9/7/15
to django...@googlegroups.com
On Mon, 07 Sep 2015 12:41:15 +0200, Hans S. Tømmerholt <ha...@opera.com>
wrote:

>
> Hello.
>
> In a nutshell:
> It appears the validator applied to a GenericIPAddressField form field
> doesn't allow an IP address with a netmask.
> But saving the same thing to a PostgreSQL database via a model works
> just fine.
>
> As far as I can tell, both model and form fields use the same
> validators? Is this somehow expected?

I'll answer some of my own questions.

GenericIPAddressField isn't intended to represent a CIDR address, which I
guess is what I'm trying to put into it.

There is stuff like this:
https://pypi.python.org/pypi/django-netfields/0.2.1

I am still slightly confused as to why validation doesn't occur on the
model level, though.

I got thrown by a different part of this app using GenericIPAddressField
for a CIDR-value, which is working fine because it's never passed through
a form.

Tom Evans

unread,
Sep 7, 2015, 9:10:56 AM9/7/15
to django...@googlegroups.com
On Mon, Sep 7, 2015 at 12:33 PM, Hans S. Tømmerholt <ha...@opera.com> wrote:
> GenericIPAddressField isn't intended to represent a CIDR address, which I
> guess is what I'm trying to put into it.
>
> There is stuff like this:
> https://pypi.python.org/pypi/django-netfields/0.2.1

+1

This is what we use for CIDR and arbitrary IP ranges, stored in
postrgres as native types so that queries/filters all happen on the DB
server with exceptional performance.

>
> I am still slightly confused as to why validation doesn't occur on the model
> level, though.


Validation only happens if you instruct it to happen. If you just
save() the model instance, no validation occurs. See the "Note ..."
paragraph here:

https://docs.djangoproject.com/en/1.8/ref/models/instances/#validating-objects

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages