Registry / database / django-netfields

django-netfields

JSON →
library1.4.1pypypiunverified

django-netfields provides robust PostgreSQL net-related fields for Django models, specifically `INET`, `CIDR`, `MACADDR`, and `MACADDR8` types. It addresses limitations of Django's built-in IP address fields by providing efficient database-native lookups and supporting IPv6. The library is actively maintained, with the current version being 1.4.1, and typically follows Django's release cycle for compatibility updates.

pip install django-netfields
INSTALL
IMPORT
SIG · DJANGO-NETFIELDS
D
django-netfields
databasepythonv1.4.1
Install
3.6s avg
Import
Disk
75MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.4.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 76.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.6s · import 0.000s · 77MB
75MB installed
● package 75MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

InetAddressField
from netfields import InetAddressField
from netfields import InetAddressField
CidrAddressField
from netfields import CidrAddressField
MACAddressField
from netfields import MACAddressField

This quickstart demonstrates defining a model with `InetAddressField`, `CidrAddressField`, and `MACAddressField`. It highlights the use of `NetManager` for advanced queries and shows how to create and retrieve objects using `ipaddress` and `netaddr` types.

import os import django from django.conf import settings from django.db import models from netfields import InetAddressField, CidrAddressField, MACAddressField, NetManager import ipaddress import netaddr # Minimal Django setup for standalone script if not settings.configured: settings.configure( INSTALLED_APPS=['netfields'], DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}, DEBUG=True ) django.setup() class NetworkDevice(models.Model): name = models.CharField(max_length=100) ip_address = InetAddressField(unique=True) network = CidrAddressField(blank=True, null=True) mac_address = MACAddressField(blank=True, null=True) objects = NetManager() def __str__(self): return f"{self.name} - {self.ip_address}" # Create database schema (in-memory for example) with django.test.utils.override_settings(MIGRATION_MODULES={'netfields': None}): from django.core.management import call_command call_command('makemigrations', 'netfields', interactive=False, verbosity=0) call_command('migrate', interactive=False, verbosity=0) # Usage examples # Create instances with ipaddress/netaddr objects device1 = NetworkDevice.objects.create( name='Router1', ip_address=ipaddress.ip_interface('192.168.1.1/24'), network=ipaddress.ip_network('192.168.1.0/24'), mac_address=netaddr.EUI('00:11:22:AA:BB:CC') ) device2 = NetworkDevice.objects.create( name='Server1', ip_address=ipaddress.ip_address('192.168.1.100'), network=ipaddress.ip_network('192.168.1.0/24') ) # Retrieve and query print(f"Device 1: {device1}") print(f"Device 2: {device2}") # Lookup using NetManager's custom lookups # Find devices within a network subnet_devices = NetworkDevice.objects.filter(ip_address__net_contained_or_equal='192.168.1.0/24') print(f"\nDevices in 192.168.1.0/24: {list(subnet_devices)}") # Find networks that contain a specific IP containing_networks = NetworkDevice.objects.filter(network__net_contains='192.168.1.50') print(f"Networks containing 192.168.1.50: {list(containing_networks)}") # Update an instance device1.ip_address = ipaddress.ip_interface('10.0.0.1/8') device1.save() print(f"Updated Device 1 IP: {device1.ip_address}")
Debug
Known issues
gotchaFor `InetAddressField` and `CidrAddressField`, you *must* use `NetManager` (e.g., `objects = NetManager()`) on your model to enable the advanced network-specific lookups like `__net_contained` or `__net_contains`.
fix
Set `objects = NetManager()` on your Django model definition when using `InetAddressField` or `CidrAddressField`.
affects: All versions
breakingVersion 1.0.0 introduced significant changes to `django-rest-framework` fields provided by `django-netfields`. Code relying on string representations for validation or pre-save logic on unsaved models might require adjustments.
fix
Review validation and pre-save hooks for `REST framework` fields. Ensure they handle `ipaddress` or `netaddr.EUI` objects directly, rather than assuming string input for network fields.
affects: >=1.0.0
gotchaBy default, `InetAddressField` in Python represents values as `ipaddress.ip_interface` objects. If you only want an `ipaddress.ip_address` object (without the prefix length), you must set `store_prefix_length=False` when defining the field.
fix
Define `inet = InetAddressField(store_prefix_length=False)` in your model if you require `ipaddress.ip_address` objects in Python.
affects: All versions
Upgrade
Version history
1.4.1latest on PyPI · released Mar 5, 2026
Audit
Dependencies
DjangorequiredRequired for Django ORM integration. Minimum Django >= 1.11.
psycopg2requiredPostgreSQL adapter for Python, required for database interaction. `psycopg` is also supported.
netaddrrequiredUsed internally for MAC address field representations (EUI types).
Agent activity
15 hits · last 30 days
node
14
Bingbot
1
Resources
django-netfields — pip install django-netfields · libregistry