README.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. Django PostgreSQL Netfields
  2. ===========================
  3. .. image:: https://secure.travis-ci.org/jimfunk/django-postgresql-netfields.png
  4. This project is an attempt at making proper PostgreSQL net related fields for
  5. Django. In Django pre 1.4 the built in ``IPAddressField`` does not support IPv6
  6. and uses an inefficient ``HOST()`` cast in all lookups. As of 1.4 you can use
  7. ``GenericIPAddressField`` for IPv6, but the casting problem remains.
  8. In addition to the basic ``IPAddressField`` replacement a ``CIDR`` and
  9. a ``MACADDR`` field have been added. This library also provides a manager that
  10. allows for advanced IP based lookup directly in the ORM.
  11. In Python, the values of these fields are represented as types from the
  12. netaddr_ module.
  13. .. _netaddr: http://pythonhosted.org/netaddr/
  14. Dependencies
  15. ------------
  16. Current version of code is targeting Django 1.3-1.4 support, as this relies
  17. heavily on ORM internals supporting multiple versions is especially tricky. The
  18. ``netaddr`` module is used for the same reasons.
  19. Getting started
  20. ---------------
  21. Make sure ``netfields`` is in your ``PYTHONPATH``.
  22. ``InetAddressField`` will store values in PostgreSQL as type ``INET``. In
  23. Python, the value will be represented as a ``netaddr.IPAddress`` object.
  24. from netfields import InetAddressField, NetManager
  25. class Example(models.Model):
  26. inet = InetAddressField()
  27. # ...
  28. objects = NetManager()
  29. ``CidrAddressField`` will store values in PostgreSQL as type ``CIDR``. In
  30. Python, the value will be represented as a ``netaddr.IPNetwork`` object.
  31. from netfields import CidrAddressField, NetManager
  32. class Example(models.Model):
  33. inet = CidrAddressField()
  34. # ...
  35. objects = NetManager()
  36. ``MACAddressField`` will store values in PostgreSQL as type ``MACADDR``. In
  37. Python, the value will be represented as a ``netaddr.EUI`` object. Note that
  38. the default text representation of EUI objects is not the same as that of the
  39. ``netaddr`` module. It is represented in a format that is more commonly used
  40. in network utilities and by network administrators (``00:11:22:aa:bb:cc``).
  41. from netfields import CidrAddressField, NetManager
  42. class Example(models.Model):
  43. inet = CidrAddressField()
  44. # ...
  45. For ``InetAddressField`` and ``CidrAddressField``, ``NetManager`` is required
  46. for the extra lookups to be available. Lookups for ``INET`` and ``CIDR``
  47. database types will be handled differently than when running vanilla Django.
  48. All lookups are case-insensitive and text based lookups are avoided whenever
  49. possible. In addition to Django's default lookup types the following have been added.
  50. * ``__net_contained``
  51. * ``__net_contained_or_equal``
  52. * ``__net_contains``
  53. * ``__net_contains_or_equals``
  54. These correspond with the operators from
  55. http://www.postgresql.org/docs/9.1/interactive/functions-net.html
  56. ``netfields`` does not have to be in ``INSTALLED_APPS``.
  57. Related Django bugs
  58. -------------------
  59. * 11442_ - Postgresql backend casts inet types to text, breaks IP operations and IPv6 lookups.
  60. * 811_ - IPv6 address field support.
  61. https://docs.djangoproject.com/en/dev/releases/1.4/#extended-ipv6-support is also relevant
  62. .. _11442: http://code.djangoproject.com/ticket/11442
  63. .. _811: http://code.djangoproject.com/ticket/811
  64. Similar projects
  65. ----------------
  66. https://bitbucket.org/onelson/django-ipyfield tries to solve some of the same
  67. issues as this library. However, instead of supporting just postgres via the proper
  68. fields types the ipyfield currently uses a ``VARCHAR(39)`` as a fake unsigned 64 bit
  69. number in its implementation.
  70. History
  71. -------
  72. Main repo was originaly kept https://github.com/adamcik/django-postgresql-netfields
  73. Late April 2013 the project was moved to https://github.com/jimfunk/django-postgresql-netfields
  74. to pass the torch on to someone who actually uses this code actively :-)