3 | | Basically, IPv4-mapped IPv6 addresses in the CIDR block ::ffff:0.0.0.0/104 are validated as invalid when unpack_ipv4=False, yet is valid when the first IPv4 octet is non-zero or when unpack_ipv4=True. |
| 3 | {{{ #!python |
| 4 | # my.app.models |
| 5 | # Django==1.8.6 |
| 6 | from django.db import models |
| 7 | class TestIPNoUnpack(models.Model): |
| 8 | ip = models.GenericIPAddressField(null=False, protocol='both', unpack_ipv4=False) |
| 9 | |
| 10 | |
| 11 | class TestIPUnpack(models.Model): |
| 12 | ip = models.GenericIPAddressField(null=False, protocol='both', unpack_ipv4=True) |
| 13 | |
| 14 | |
| 15 | |
| 16 | > python manage.py shell |
| 17 | Python 2.7.6 (default, Sep 9 2014, 15:04:36) |
| 18 | Type "copyright", "credits" or "license" for more information. |
| 19 | |
| 20 | IPython 4.0.1 -- An enhanced Interactive Python. |
| 21 | ? -> Introduction and overview of IPython's features. |
| 22 | %quickref -> Quick reference. |
| 23 | help -> Python's own help system. |
| 24 | object? -> Details about 'object', use 'object??' for extra details. |
| 25 | |
| 26 | In [1]: from my.app.models import TestIPNoUnpack, TestIPUnpack |
| 27 | |
| 28 | In [2]: testipunpack = TestIPUnpack(ip='::ffff:0.0.0.0') |
| 29 | |
| 30 | In [3]: testipunpack.clean_fields() |
| 31 | |
| 32 | In [4]: testipunpack.ip |
| 33 | Out[4]: '0.0.0.0' |
| 34 | |
| 35 | In [5]: testipnounpack = TestIPNoUnpack(ip='::ffff:0.0.0.0') |
| 36 | |
| 37 | In [6]: testipnounpack.clean_fields() |
| 38 | --------------------------------------------------------------------------- |
| 39 | ValidationError Traceback (most recent call last) |
| 40 | <ipython-input-6-9a763f56e907> in <module>() |
| 41 | ----> 1 testipnounpack.clean_fields() |
| 42 | |
| 43 | .../lib/python2.7/site-packages/django/db/models/base.pyc in clean_fields(self, exclude) |
| 44 | 1194 |
| 45 | 1195 if errors: |
| 46 | -> 1196 raise ValidationError(errors) |
| 47 | 1197 |
| 48 | 1198 @classmethod |
| 49 | |
| 50 | ValidationError: {'ip': [u'Enter a valid IPv4 or IPv6 address.']} |
| 51 | |
| 52 | In [7]: testipnounpack2 = TestIPNoUnpack(ip='::ffff:0.255.255.255') |
| 53 | |
| 54 | In [8]: testipnounpack2.clean_fields() |
| 55 | --------------------------------------------------------------------------- |
| 56 | ValidationError Traceback (most recent call last) |
| 57 | <ipython-input-8-e228d0855286> in <module>() |
| 58 | ----> 1 testipnounpack2.clean_fields() |
| 59 | |
| 60 | .../lib/python2.7/site-packages/django/db/models/base.pyc in clean_fields(self, exclude) |
| 61 | 1194 |
| 62 | 1195 if errors: |
| 63 | -> 1196 raise ValidationError(errors) |
| 64 | 1197 |
| 65 | 1198 @classmethod |
| 66 | |
| 67 | ValidationError: {'ip': [u'Enter a valid IPv4 or IPv6 address.']} |
| 68 | |
| 69 | In [9]: testipnounpack3 = TestIPNoUnpack(ip='::ffff:1.0.0.0') |
| 70 | |
| 71 | In [10]: testipnounpack3.clean_fields() |
| 72 | |
| 73 | In [11]: testipnounpack3.ip |
| 74 | Out[11]: '::ffff:1.0.0.0' |
| 75 | |
| 76 | }}} |