Fixes #19520: restores ability to set Prefix.scope via API (#19588)

This commit is contained in:
Jason Novinger 2025-05-27 12:32:36 -05:00 committed by GitHub
parent a97b438b7e
commit cc099e86e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View File

@ -85,7 +85,7 @@ class CachedScopeMixin(models.Model):
abstract = True
def clean(self):
if self.scope_type and not self.scope:
if self.scope_type and not (self.scope or self.scope_id):
scope_type = self.scope_type.model_class()
raise ValidationError({
'scope': _(

View File

@ -1,6 +1,7 @@
import json
import logging
from django.test import tag
from django.urls import reverse
from netaddr import IPNetwork
from rest_framework import status
@ -383,6 +384,18 @@ class PrefixTest(APIViewTestCases.APIViewTestCase):
)
Prefix.objects.bulk_create(prefixes)
@tag('regression')
def test_clean_validates_scope(self):
prefix = Prefix.objects.first()
site = Site.objects.create(name='Test Site', slug='test-site')
data = {'scope_type': 'dcim.site', 'scope_id': site.id}
url = reverse('ipam-api:prefix-detail', kwargs={'pk': prefix.pk})
self.add_permissions('ipam.change_prefix')
response = self.client.patch(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_200_OK)
def test_list_available_prefixes(self):
"""
Test retrieval of all available prefixes within a parent prefix.