Skip to content

Commit 3e7a488

Browse files
committed
Fix "Converter is already registered" deprecation warning.
1 parent f113ab6 commit 3e7a488

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

rest_framework/urlpatterns.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
from django.urls import URLResolver, include, path, re_path, register_converter
2+
from django.urls.converters import get_converters
23
from django.urls.resolvers import RoutePattern
34

45
from rest_framework.settings import api_settings
56

67

78
def _get_format_path_converter(suffix_kwarg, allowed):
9+
converter_name = 'drf_format_suffix'
10+
if allowed:
11+
converter_name += '_' + '_'.join(allowed)
12+
13+
if converter_name in get_converters():
14+
return converter_name
15+
816
if allowed:
917
if len(allowed) == 1:
1018
allowed_pattern = allowed[0]
@@ -23,11 +31,9 @@ def to_python(self, value):
2331
def to_url(self, value):
2432
return '.' + value + '/'
2533

26-
converter_name = 'drf_format_suffix'
27-
if allowed:
28-
converter_name += '_' + '_'.join(allowed)
34+
register_converter(FormatSuffixConverter, converter_name)
2935

30-
return converter_name, FormatSuffixConverter
36+
return converter_name
3137

3238

3339
def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required, suffix_route=None):
@@ -104,8 +110,7 @@ def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None):
104110
else:
105111
suffix_pattern = r'\.(?P<%s>[a-z0-9]+)/?$' % suffix_kwarg
106112

107-
converter_name, suffix_converter = _get_format_path_converter(suffix_kwarg, allowed)
108-
register_converter(suffix_converter, converter_name)
113+
converter_name = _get_format_path_converter(suffix_kwarg, allowed)
109114

110115
suffix_route = '<%s:%s>' % (converter_name, suffix_kwarg)
111116

0 commit comments

Comments
 (0)