Convert interface models to use NaturalOrderingField

This commit is contained in:
Jeremy Stretch
2020-02-07 15:47:53 -05:00
parent 96ceac53e2
commit 14b3d74903
5 changed files with 120 additions and 65 deletions

View File

@@ -4,9 +4,9 @@ from django.db import models
from dcim.choices import *
from dcim.constants import *
from dcim.managers import InterfaceManager
from extras.models import ObjectChange
from utilities.fields import NaturalOrderingField
from utilities.ordering import naturalize_interface
from utilities.utils import serialize_object
from .device_components import (
ConsolePort, ConsoleServerPort, DeviceBay, FrontPort, Interface, PowerOutlet, PowerPort, RearPort,
@@ -249,6 +249,12 @@ class InterfaceTemplate(ComponentTemplateModel):
name = models.CharField(
max_length=64
)
_name = NaturalOrderingField(
target_field='name',
naturalize_function=naturalize_interface,
max_length=100,
blank=True
)
type = models.CharField(
max_length=50,
choices=InterfaceTypeChoices
@@ -258,11 +264,9 @@ class InterfaceTemplate(ComponentTemplateModel):
verbose_name='Management only'
)
objects = InterfaceManager()
class Meta:
ordering = ['device_type', 'name']
unique_together = ['device_type', 'name']
ordering = ('device_type', '_name')
unique_together = ('device_type', 'name')
def __str__(self):
return self.name

View File

@@ -10,9 +10,9 @@ from dcim.choices import *
from dcim.constants import *
from dcim.exceptions import LoopDetected
from dcim.fields import MACAddressField
from dcim.managers import InterfaceManager
from extras.models import ObjectChange, TaggedItem
from utilities.fields import NaturalOrderingField
from utilities.ordering import naturalize_interface
from utilities.utils import serialize_object
from virtualization.choices import VMInterfaceTypeChoices
@@ -529,6 +529,12 @@ class Interface(CableTermination, ComponentModel):
name = models.CharField(
max_length=64
)
_name = NaturalOrderingField(
target_field='name',
naturalize_function=naturalize_interface,
max_length=100,
blank=True
)
_connected_interface = models.OneToOneField(
to='self',
on_delete=models.SET_NULL,
@@ -597,8 +603,6 @@ class Interface(CableTermination, ComponentModel):
blank=True,
verbose_name='Tagged VLANs'
)
objects = InterfaceManager()
tags = TaggableManager(through=TaggedItem)
csv_headers = [
@@ -607,8 +611,9 @@ class Interface(CableTermination, ComponentModel):
]
class Meta:
ordering = ['device', 'name']
unique_together = ['device', 'name']
# TODO: ordering and unique_together should include virtual_machine
ordering = ('device', '_name')
unique_together = ('device', 'name')
def __str__(self):
return self.name