From 60f90f83eaef46dc9f932136ac900174f4377fdd Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Tue, 5 Dec 2023 03:34:31 +0800 Subject: [PATCH 1/2] Fix use of ip_address_validators for Django 5.0+ --- rest_framework/compat.py | 15 +++++++++++++++ rest_framework/fields.py | 5 +++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 7e80704e11..5858042a6f 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -169,6 +169,21 @@ def parse_header_parameters(line): } +if django.VERSION >= (5, 0): + # Django 5.0+: use the stock ip_address_validators function + # Note: Before Django 5.0, ip_address_validators returns a tuple containing + # 1) the list of validators and 2) the error message. Starting from + # Django 5.0 ip_address_validators only returns the list of validators + from django.core.validators import ip_address_validators +else: + # Django <= 5.0: create a compatibility shim for ip_address_validators + from django.core.validators import \ + ip_address_validators as _ip_address_validators + + def ip_address_validators(protocol, unpack_ipv4): + return _ip_address_validators(protocol, unpack_ipv4)[0] + + # `separators` argument to `json.dumps()` differs between 2.x and 3.x # See: https://bugs.python.org/issue22767 SHORT_SEPARATORS = (',', ':') diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 0b56fa7fb6..fda656507b 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -16,7 +16,7 @@ from django.core.validators import ( EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator, MinValueValidator, ProhibitNullCharactersValidator, RegexValidator, - URLValidator, ip_address_validators + URLValidator ) from django.forms import FilePathField as DjangoFilePathField from django.forms import ImageField as DjangoImageField @@ -36,6 +36,7 @@ pytz = None from rest_framework import ISO_8601 +from rest_framework.compat import ip_address_validators from rest_framework.exceptions import ErrorDetail, ValidationError from rest_framework.settings import api_settings from rest_framework.utils import html, humanize_datetime, json, representation @@ -866,7 +867,7 @@ def __init__(self, protocol='both', **kwargs): self.protocol = protocol.lower() self.unpack_ipv4 = (self.protocol == 'both') super().__init__(**kwargs) - validators, error_message = ip_address_validators(protocol, self.unpack_ipv4) + validators = ip_address_validators(protocol, self.unpack_ipv4) self.validators.extend(validators) def to_internal_value(self, data): From 1112ffefe98d0b1f3300339705d51c2b9c0d015a Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Thu, 14 Dec 2023 19:06:00 +0800 Subject: [PATCH 2/2] Change affected django version to 5.1 --- rest_framework/compat.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 5858042a6f..472b8ad244 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -169,14 +169,14 @@ def parse_header_parameters(line): } -if django.VERSION >= (5, 0): - # Django 5.0+: use the stock ip_address_validators function - # Note: Before Django 5.0, ip_address_validators returns a tuple containing +if django.VERSION >= (5, 1): + # Django 5.1+: use the stock ip_address_validators function + # Note: Before Django 5.1, ip_address_validators returns a tuple containing # 1) the list of validators and 2) the error message. Starting from - # Django 5.0 ip_address_validators only returns the list of validators + # Django 5.1 ip_address_validators only returns the list of validators from django.core.validators import ip_address_validators else: - # Django <= 5.0: create a compatibility shim for ip_address_validators + # Django <= 5.1: create a compatibility shim for ip_address_validators from django.core.validators import \ ip_address_validators as _ip_address_validators