Hi I have the following Django model
class AccessPointIPAddress(models.Model):
'''Model for storing AccessPoint IP Addresses.'''
ap = models.ForeignKey(AccessPoint, related_name='ip_addresses')
ip_address = models.GenericIPAddressField(protocol='IPv4')
datetime = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['datetime']
get_latest_by = 'datetime'
And I am assuming that django's GenericIPAddressField
does some string validation that a string is indeed a valid IP Address. I also read django's source and it does have some validation functions tied to GenericIPAddressField
But when I try to run this on django's shell:
# Assume that *ap* is a valid AccessPoint instance
# Notice ip_address IS NOT A VALID IP ADDRESS
>> AccessPointIPAddress.objects.create(ap=ap, ip_address='xxxxxx123123----')
<AccessPointIPAddress: ap xxxxxx123123---- 2015-05-18 12:39:25.491811>
I am expecting that it should raise some kind of ValueError or validation error since the given ip-address xxxxxx123123----
is not a valid ip address.
Am I missing something here? Or is this part of django broken? Currently using Django 1.6.5
https://docs.djangoproject.com/en/1.8/ref/validators/#how-validators-are-run
You can override save() method and do a full_clean() on the model instance as described in the docs here: https://docs.djangoproject.com/en/1.8/ref/validators/#how-validators-are-run
or only use validator for GenericIPAddressField:
it will use the following validator: