mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-17 04:32:51 -06:00
Move IPAddressManager to a separate file
This commit is contained in:
parent
6959785cd1
commit
c084547dca
16
netbox/ipam/managers.py
Normal file
16
netbox/ipam/managers.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from django.db import models
|
||||||
|
from django.db.models.expressions import RawSQL
|
||||||
|
|
||||||
|
|
||||||
|
class IPAddressManager(models.Manager):
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""
|
||||||
|
By default, PostgreSQL will order INETs with shorter (larger) prefix lengths ahead of those with longer
|
||||||
|
(smaller) masks. This makes no sense when ordering IPs, which should be ordered solely by family and host
|
||||||
|
address. We can use HOST() to extract just the host portion of the address (ignoring its mask), but we must
|
||||||
|
then re-cast this value to INET() so that records will be ordered properly. We are essentially re-casting each
|
||||||
|
IP address as a /32 or /128.
|
||||||
|
"""
|
||||||
|
qs = super().get_queryset()
|
||||||
|
return qs.annotate(host=RawSQL('INET(HOST(ipam_ipaddress.address))', [])).order_by('family', 'host')
|
@ -5,7 +5,6 @@ from django.core.exceptions import ValidationError, ObjectDoesNotExist
|
|||||||
from django.core.validators import MaxValueValidator, MinValueValidator
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import F, Q
|
from django.db.models import F, Q
|
||||||
from django.db.models.expressions import RawSQL
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from taggit.managers import TaggableManager
|
from taggit.managers import TaggableManager
|
||||||
|
|
||||||
@ -17,6 +16,7 @@ from virtualization.models import VirtualMachine
|
|||||||
from .choices import *
|
from .choices import *
|
||||||
from .constants import IPADDRESS_ROLES_NONUNIQUE
|
from .constants import IPADDRESS_ROLES_NONUNIQUE
|
||||||
from .fields import IPNetworkField, IPAddressField
|
from .fields import IPNetworkField, IPAddressField
|
||||||
|
from .managers import IPAddressManager
|
||||||
from .querysets import PrefixQuerySet
|
from .querysets import PrefixQuerySet
|
||||||
from .validators import DNSValidator
|
from .validators import DNSValidator
|
||||||
|
|
||||||
@ -558,20 +558,6 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
|
|||||||
return int(float(child_count) / prefix_size * 100)
|
return int(float(child_count) / prefix_size * 100)
|
||||||
|
|
||||||
|
|
||||||
class IPAddressManager(models.Manager):
|
|
||||||
|
|
||||||
def get_queryset(self):
|
|
||||||
"""
|
|
||||||
By default, PostgreSQL will order INETs with shorter (larger) prefix lengths ahead of those with longer
|
|
||||||
(smaller) masks. This makes no sense when ordering IPs, which should be ordered solely by family and host
|
|
||||||
address. We can use HOST() to extract just the host portion of the address (ignoring its mask), but we must
|
|
||||||
then re-cast this value to INET() so that records will be ordered properly. We are essentially re-casting each
|
|
||||||
IP address as a /32 or /128.
|
|
||||||
"""
|
|
||||||
qs = super().get_queryset()
|
|
||||||
return qs.annotate(host=RawSQL('INET(HOST(ipam_ipaddress.address))', [])).order_by('family', 'host')
|
|
||||||
|
|
||||||
|
|
||||||
class IPAddress(ChangeLoggedModel, CustomFieldModel):
|
class IPAddress(ChangeLoggedModel, CustomFieldModel):
|
||||||
"""
|
"""
|
||||||
An IPAddress represents an individual IPv4 or IPv6 address and its mask. The mask length should match what is
|
An IPAddress represents an individual IPv4 or IPv6 address and its mask. The mask length should match what is
|
||||||
|
Loading…
Reference in New Issue
Block a user