Skip to content

Commit 914653d

Browse files
jnovingerjeremystretch
authored andcommitted
Fixes #21045: Allow saving Site with associated Prefix
This was a result of the fix for #20944 optimizing a query to only include the `id` field with `.only(id)`. Since `Prefix.__init__()` caches original values from other fields (`_prefix` and `_vrf_id`), these cached values are `None` at init-time. This might not normally be a problem, but the sequence of events in the bug report also end up causing the `handle_prefix_saved` handler to run, which uses an ORM lookup, (either `net_contained_or_equal` original`net_contained`) that does not support a query argument of `None`.
1 parent 3813aad commit 914653d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

netbox/dcim/signals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def sync_cached_scope_fields(instance, created, **kwargs):
210210
for model in (Prefix, Cluster, WirelessLAN):
211211
qs = model.objects.filter(**filters)
212212

213-
for obj in qs.only('id'):
213+
for obj in qs:
214214
# Recompute cache using the same logic as save()
215215
obj.cache_related_objects()
216216
obj.save(update_fields=[

netbox/dcim/tests/test_models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from dcim.choices import *
77
from dcim.models import *
88
from extras.models import CustomField
9+
from ipam.models import Prefix
910
from netbox.choices import WeightUnitChoices
1011
from tenancy.models import Tenant
1112
from utilities.data import drange
@@ -1192,3 +1193,14 @@ def test_virtualchassis_duplicate_vc_position(self):
11921193
device2.vc_position = 1
11931194
with self.assertRaises(ValidationError):
11941195
device2.full_clean()
1196+
1197+
1198+
class SiteSignalTestCase(TestCase):
1199+
1200+
@tag('regression')
1201+
def test_edit_site_with_prefix_no_vrf(self):
1202+
site = Site.objects.create(name='Test Site', slug='test-site')
1203+
Prefix.objects.create(prefix='192.0.2.0/24', scope=site, vrf=None)
1204+
1205+
# Regression test for #21045: should not raise ValueError
1206+
site.save()

0 commit comments

Comments
 (0)