mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-16 16:52:17 -06:00
Compare commits
1 Commits
main
...
21203-q-at
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5dd5d65d74 |
@@ -29,6 +29,15 @@ class DCIMConfig(AppConfig):
|
||||
denormalized.register(CableTermination, '_location', {
|
||||
'_site': 'site',
|
||||
})
|
||||
denormalized.register(Device, 'virtual_chassis', {
|
||||
'_virtual_chassis_name': 'name',
|
||||
})
|
||||
denormalized.register(Device, 'primary_ip4', {
|
||||
'_primary_ip4_address': 'address',
|
||||
})
|
||||
denormalized.register(Device, 'primary_ip6', {
|
||||
'_primary_ip6_address': 'address',
|
||||
})
|
||||
|
||||
# Register counters
|
||||
connect_counters(Device, DeviceType, ModuleType, RackType, VirtualChassis)
|
||||
|
||||
@@ -1331,13 +1331,14 @@ class DeviceFilterSet(
|
||||
return queryset
|
||||
return queryset.filter(
|
||||
Q(name__icontains=value) |
|
||||
Q(virtual_chassis__name__icontains=value) |
|
||||
Q(serial__icontains=value.strip()) |
|
||||
Q(asset_tag__icontains=value.strip()) |
|
||||
Q(description__icontains=value.strip()) |
|
||||
Q(comments__icontains=value) |
|
||||
Q(primary_ip4__address__startswith=value) |
|
||||
Q(primary_ip6__address__startswith=value)
|
||||
# Denormalized fields
|
||||
Q(_virtual_chassis_name__icontains=value) |
|
||||
Q(_primary_ip4_address__startswith=value) |
|
||||
Q(_primary_ip6_address__startswith=value)
|
||||
).distinct()
|
||||
|
||||
def _has_primary_ip(self, queryset, name, value):
|
||||
|
||||
@@ -224,7 +224,7 @@ class ConsoleServerPortTemplateType(ModularComponentTemplateType):
|
||||
|
||||
@strawberry_django.type(
|
||||
models.Device,
|
||||
fields='__all__',
|
||||
exclude=['_virtual_chassis_name', '_primary_ip4_address', '_primary_ip6_address'],
|
||||
filters=DeviceFilter,
|
||||
pagination=True
|
||||
)
|
||||
|
||||
46
netbox/dcim/migrations/0226_denormalize_search_attrs.py
Normal file
46
netbox/dcim/migrations/0226_denormalize_search_attrs.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from django.db import migrations, models
|
||||
from django.db.models import Q
|
||||
|
||||
import ipam.fields
|
||||
|
||||
|
||||
def backfill_denormalized_fields(apps, schema_editor):
|
||||
Device = apps.get_model('dcim', 'Device')
|
||||
|
||||
# TODO: Optimize for bulk operations
|
||||
for device in Device.objects.filter(
|
||||
Q(virtual_chassis__isnull=False) | Q(primary_ip4__isnull=False) | Q(primary_ip6__isnull=False)
|
||||
):
|
||||
device._virtual_chassis_name = device.virtual_chassis.name if device.virtual_chassis else ''
|
||||
device._primary_ip4_address = device.primary_ip4.address if device.primary_ip4 else None
|
||||
device._primary_ip6_address = device.primary_ip6.address if device.primary_ip6 else None
|
||||
device.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('dcim', '0225_gfk_indexes'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='device',
|
||||
name='_primary_ip4_address',
|
||||
field=ipam.fields.IPAddressField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='device',
|
||||
name='_primary_ip6_address',
|
||||
field=ipam.fields.IPAddressField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='device',
|
||||
name='_virtual_chassis_name',
|
||||
field=models.CharField(blank=True),
|
||||
),
|
||||
migrations.RunPython(
|
||||
code=backfill_denormalized_fields,
|
||||
reverse_code=migrations.RunPython.noop
|
||||
),
|
||||
]
|
||||
@@ -21,6 +21,7 @@ from dcim.fields import MACAddressField
|
||||
from dcim.utils import create_port_mappings, update_interface_bridges
|
||||
from extras.models import ConfigContextModel, CustomField
|
||||
from extras.querysets import ConfigContextModelQuerySet
|
||||
from ipam.fields import IPAddressField
|
||||
from netbox.choices import ColorChoices
|
||||
from netbox.config import ConfigItem
|
||||
from netbox.models import NestedGroupModel, OrganizationalModel, PrimaryModel
|
||||
@@ -673,6 +674,19 @@ class Device(
|
||||
related_query_name='device',
|
||||
)
|
||||
|
||||
# Denormalized fields
|
||||
_virtual_chassis_name = models.CharField(
|
||||
blank=True,
|
||||
)
|
||||
_primary_ip4_address = IPAddressField(
|
||||
blank=True,
|
||||
null=True,
|
||||
)
|
||||
_primary_ip6_address = IPAddressField(
|
||||
blank=True,
|
||||
null=True,
|
||||
)
|
||||
|
||||
# Counter fields
|
||||
console_port_count = CounterCacheField(
|
||||
to_model='dcim.ConsolePort',
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
from django import forms
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from utilities.forms.widgets.apiselect import APISelect, APISelectMultiple
|
||||
|
||||
__all__ = (
|
||||
'FilterModifierWidget',
|
||||
'MODIFIER_EMPTY_FALSE',
|
||||
@@ -96,37 +94,9 @@ class FilterModifierWidget(forms.Widget):
|
||||
# to the original widget before rendering
|
||||
self.original_widget.attrs.update(self.attrs)
|
||||
|
||||
# For APISelect/APISelectMultiple widgets, temporarily clear choices to prevent queryset evaluation
|
||||
original_choices = None
|
||||
if isinstance(self.original_widget, (APISelect, APISelectMultiple)):
|
||||
original_choices = self.original_widget.choices
|
||||
|
||||
# Only keep selected choices to preserve current selection in HTML
|
||||
if value:
|
||||
values = value if isinstance(value, (list, tuple)) else [value]
|
||||
|
||||
if hasattr(original_choices, 'queryset'):
|
||||
queryset = original_choices.queryset
|
||||
selected_objects = queryset.filter(pk__in=values)
|
||||
# Build minimal choice list with just the selected values
|
||||
self.original_widget.choices = [
|
||||
(obj.pk, str(obj)) for obj in selected_objects
|
||||
]
|
||||
else:
|
||||
self.original_widget.choices = [
|
||||
choice for choice in original_choices if choice[0] in values
|
||||
]
|
||||
else:
|
||||
# No selection - render empty select element
|
||||
self.original_widget.choices = []
|
||||
|
||||
# Get context from the original widget
|
||||
original_context = self.original_widget.get_context(name, value, attrs)
|
||||
|
||||
# Restore original choices if we modified them
|
||||
if original_choices is not None:
|
||||
self.original_widget.choices = original_choices
|
||||
|
||||
# Build our wrapper context
|
||||
context = super().get_context(name, value, attrs)
|
||||
context['widget']['original_widget'] = original_context['widget']
|
||||
|
||||
Reference in New Issue
Block a user