Use django-postgresql-netfields instead of custom implementation #479

Closed
opened 2025-12-29 16:22:32 +01:00 by adam · 1 comment
Owner

Originally created by @jsenecal on GitHub (Oct 19, 2016).

While working with the django models and custom management commands, I realized that netbox was using it's own implementation of Postgresql's ip/network fields.

This implementation lacks some functionality present in https://github.com/jimfunk/django-postgresql-netfields

For instance :

In [1]: from netaddr import *

In [2]: from ipam.models import *

In [3]: prefix, created = Prefix.objects.get_or_create(prefix='10.0.0.0/8')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<REDACTED>
AttributeError: 'str' object has no attribute 'cidr'

In [4]: prefix, created = Prefix.objects.get_or_create(prefix=IPNetwork('10.0.0.0/8'))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<REDACTED>
TypeError: unexpected type <type 'list'> for addr arg

Doing the following works but kinda breaks the Django magic:

In [1]: from netaddr import *

In [2]: from ipam.models import *

In [3]: try:
   ...:     prefix = Prefix.objects.get(prefix='10.0.0.0/8')
   ...: except Prefix.DoesNotExist:
   ...:     do something...
   ...: else:
   ...:     do something else
   ...:

https://github.com/jimfunk/django-postgresql-netfields is a good maintained implementation and it would help solve issues like #595

Originally created by @jsenecal on GitHub (Oct 19, 2016). While working with the django models and custom management commands, I realized that netbox was using it's own implementation of Postgresql's ip/network fields. This implementation lacks some functionality present in https://github.com/jimfunk/django-postgresql-netfields For instance : ``` python In [1]: from netaddr import * In [2]: from ipam.models import * In [3]: prefix, created = Prefix.objects.get_or_create(prefix='10.0.0.0/8') --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <REDACTED> AttributeError: 'str' object has no attribute 'cidr' In [4]: prefix, created = Prefix.objects.get_or_create(prefix=IPNetwork('10.0.0.0/8')) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <REDACTED> TypeError: unexpected type <type 'list'> for addr arg ``` Doing the following works but kinda breaks the Django magic: ``` python In [1]: from netaddr import * In [2]: from ipam.models import * In [3]: try: ...: prefix = Prefix.objects.get(prefix='10.0.0.0/8') ...: except Prefix.DoesNotExist: ...: do something... ...: else: ...: do something else ...: ``` https://github.com/jimfunk/django-postgresql-netfields is a good maintained implementation and it would help solve issues like #595
adam closed this issue 2025-12-29 16:22:32 +01:00
Author
Owner

@jeremystretch commented on GitHub (Oct 19, 2016):

NetBox was originally written to use django-postgresql-netfields. I moved away from it early on because it employs PostgreSQL's inet field type for IP addresses, which does not support the inclusion of a mask. NetBox instead uses the cidr type for both prefixes and addresses (but enforces zeroed host bits for prefixes). There are probably some other minor differences between the two implementations which I've forgotten as well.

The get_or_create convenience method isn't supported for Prefix objects because it bypasses validation.

@jeremystretch commented on GitHub (Oct 19, 2016): NetBox was originally written to use django-postgresql-netfields. I moved away from it early on because it employs PostgreSQL's `inet` field type for IP addresses, which does not support the inclusion of a mask. NetBox instead uses the `cidr` type for both prefixes and addresses (but enforces zeroed host bits for prefixes). There are probably some other minor differences between the two implementations which I've forgotten as well. The `get_or_create` convenience method isn't supported for Prefix objects because it bypasses validation.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#479