Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Django PostgreSQL netfields implementation

Project description

This project is an attempt at making proper PostgreSQL net related fields for Django. In Django pre 1.4 the built in IPAddressField does not support IPv6 and uses an inefficient HOST() cast in all lookups. As of 1.4 you can use GenericIPAddressField for IPv6, but the casting problem remains.

In addition to the basic IPAddressField replacement a CIDR and a MACADDR field have been added. This library also provides a manager that allows for advanced IP based lookup directly in the ORM.

In Python, the values of the IP address fields are represented as types from the ipaddress module. In Python 2.x, a backport is used. The MAC address field is represented as an EUI type from the netaddr module.


Current version of code is targeting Django >= 1.8 support, as this relies heavily on ORM internals and supporting multiple versions is especially tricky.

Getting started

Make sure netfields is in your PYTHONPATH and in INSTALLED_APPS.

InetAddressField will store values in PostgreSQL as type INET. In Python, the value will be represented as an ipaddress.ip_interface object representing an IP address and netmask/prefix length pair unless the store_prefix_length argument is set to False`, in which case the value will be represented as an ipaddress.ip_address object.

from netfields import InetAddressField, NetManager

class Example(models.Model):
    inet = InetAddressField()
    # ...

    objects = NetManager()

CidrAddressField will store values in PostgreSQL as type CIDR. In Python, the value will be represented as an ipaddress.ip_network object.

from netfields import CidrAddressField, NetManager

class Example(models.Model):
    inet = CidrAddressField()
    # ...

    objects = NetManager()

MACAddressField will store values in PostgreSQL as type MACADDR. In Python, the value will be represented as a netaddr.EUI object. Note that the default text representation of EUI objects is not the same as that of the netaddr module. It is represented in a format that is more commonly used in network utilities and by network administrators (00:11:22:aa:bb:cc).

from netfields import MACAddressField, NetManager

class Example(models.Model):
    inet = MACAddressField()
    # ...

For InetAddressField and CidrAddressField, NetManager is required for the extra lookups to be available. Lookups for INET and CIDR database types will be handled differently than when running vanilla Django. All lookups are case-insensitive and text based lookups are avoided whenever possible. In addition to Django’s default lookup types the following have been added:

is contained within the given network
is contained within or equal to the given network
contains the given address
contains or is equal to the given address/network
contains or contained by the given address
matches the given address family
matches the host part of an address regardless of prefix length

These correspond with the operators and functions from

CidrAddressField includes two extra lookups:

Maximum value (inclusive) for CIDR prefix, does not distinguish between IPv4 and IPv6
Minimum value (inclusive) for CIDR prefix, does not distinguish between IPv4 and IPv6

As of Django 2.2, indexes can be created for InetAddressField and CidrAddressField extra lookups directly on the model.

from django.contrib.postgres.indexes import GistIndex
from netfields import CidrAddressField, NetManager

class Example(models.Model):
    inet = CidrAddressField()
    # ...

    class Meta:
        indexes = (
                fields=('inet',), opclasses=('inet_ops',),

For earlier versions of Django, a custom migration can be used to install an index.

from django.db import migrations

class Migration(migrations.Migration):
    # ...

    operations = [
        # ...
            "CREATE INDEX app_example_inet_idx ON app_example USING GIST (inet inet_ops);"
        # ...


Similar projects tries to solve some of the same issues as this library. However, instead of supporting just postgres via the proper fields types the ipyfield currently uses a VARCHAR(39) as a fake unsigned 64 bit number in its implementation.


Main repo was originally kept Late April 2013 the project was moved to to pass the torch on to someone who actually uses this code actively :-)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-netfields, version 1.1.1
Filename, size File type Python version Upload date Hashes
Filename, size django-netfields-1.1.1.tar.gz (16.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page