Przeglądaj źródła

Closes #23508: Upgrade to Django REST Framework 3.18.3 and remove the get_unique_validators() monkey-patch (#23509)

DRF 3.18.2 includes an upstream fix for the uniqueness validation issue
the monkey-patch worked around (#19302), so it is no longer needed.
Jeremy Stretch 1 dzień temu
rodzic
commit
58b22d9931
4 zmienionych plików z 2 dodań i 49 usunięć
  1. 1 2
      base_requirements.txt
  2. 0 39
      netbox/netbox/monkey.py
  3. 0 7
      netbox/netbox/settings.py
  4. 1 1
      requirements.txt

+ 1 - 2
base_requirements.txt

@@ -69,8 +69,7 @@ django-timezone-field
 
 # A REST API framework for Django projects
 # https://www.django-rest-framework.org/community/release-notes/
-# TODO: Re-evaluate the monkey-patch of get_unique_validators() before upgrading
-djangorestframework==3.18.0
+djangorestframework>=3.18.2
 
 # Sane and flexible OpenAPI 3 schema generation for Django REST framework.
 # https://github.com/tfranzel/drf-spectacular/blob/master/CHANGELOG.rst

+ 0 - 39
netbox/netbox/monkey.py

@@ -1,39 +0,0 @@
-from django.db.models import UniqueConstraint
-from rest_framework.utils.field_mapping import get_unique_error_message
-from rest_framework.validators import UniqueValidator
-
-__all__ = (
-    'get_unique_validators',
-)
-
-
-def get_unique_validators(field_name, model_field):
-    """
-    Extend Django REST Framework's get_unique_validators() function to attach a UniqueValidator to a field *only* if the
-     associated UniqueConstraint does NOT have a condition which references another field. See bug #19302.
-    """
-    field_set = {field_name}
-    conditions = {
-        c.condition
-        for c in model_field.model._meta.constraints
-        if isinstance(c, UniqueConstraint) and set(c.fields) == field_set
-    }
-
-    # START custom logic
-    conditions = {
-        cond for cond in conditions
-        if cond is None or cond.referenced_base_fields == field_set
-    }
-    # END custom logic
-
-    if getattr(model_field, 'unique', False):
-        conditions.add(None)
-    if not conditions:
-        return
-    unique_error_message = get_unique_error_message(model_field)
-    queryset = model_field.model._default_manager
-    for condition in conditions:
-        yield UniqueValidator(
-            queryset=queryset if condition is None else queryset.filter(condition),
-            message=unique_error_message
-        )

+ 0 - 7
netbox/netbox/settings.py

@@ -30,8 +30,6 @@ from utilities.release import load_release_data
 from utilities.security import validate_peppers
 from utilities.string import trailing_slash
 
-from .monkey import get_unique_validators
-
 #
 # Environment setup
 #
@@ -1044,7 +1042,6 @@ for plugin_name in PLUGINS:
 #
 
 from django.db import models  # noqa: E402
-from rest_framework.utils import field_mapping  # noqa: E402
 from strawberry.schema.types.scalar import DEFAULT_SCALAR_REGISTRY  # noqa: E402
 from strawberry_django import pagination  # noqa: E402
 from strawberry_django.fields.field import StrawberryDjangoField  # noqa: E402
@@ -1053,10 +1050,6 @@ from strawberry_django.fields.types import field_type_map  # noqa: E402
 from netbox.graphql.pagination import OffsetPaginationInput, apply_pagination  # noqa: E402
 from netbox.graphql.scalars import BigInt, BigIntScalar  # noqa: E402
 
-# TODO: Remove this once #20547 has been implemented
-# Override DRF's get_unique_validators() function with our own (see bug #19302)
-field_mapping.get_unique_validators = get_unique_validators
-
 # Override strawberry-django's OffsetPaginationInput class to add the `start` parameter
 pagination.OffsetPaginationInput = OffsetPaginationInput
 

+ 1 - 1
requirements.txt

@@ -14,7 +14,7 @@ django-storages==1.14.6
 django-tables2==3.0.1
 django-taggit==6.1.0
 django-timezone-field==7.2.2
-djangorestframework==3.18.0
+djangorestframework==3.18.3
 drf-spectacular==0.30.0
 drf-spectacular-sidecar==2026.10.1
 feedparser==6.0.14