Skip to content

Commit ef026ec

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

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

rest_framework/urlpatterns.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
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

7-
def _get_format_path_converter(suffix_kwarg, allowed):
8+
def _get_format_path_converter(allowed):
89
if allowed:
910
if len(allowed) == 1:
1011
allowed_pattern = allowed[0]
@@ -23,11 +24,14 @@ def to_python(self, value):
2324
def to_url(self, value):
2425
return '.' + value + '/'
2526

27+
return FormatSuffixConverter
28+
29+
30+
def _generate_converter_name(allowed):
2631
converter_name = 'drf_format_suffix'
2732
if allowed:
2833
converter_name += '_' + '_'.join(allowed)
29-
30-
return converter_name, FormatSuffixConverter
34+
return converter_name
3135

3236

3337
def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required, suffix_route=None):
@@ -104,8 +108,10 @@ def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None):
104108
else:
105109
suffix_pattern = r'\.(?P<%s>[a-z0-9]+)/?$' % suffix_kwarg
106110

107-
converter_name, suffix_converter = _get_format_path_converter(suffix_kwarg, allowed)
108-
register_converter(suffix_converter, converter_name)
111+
converter_name = _generate_converter_name(allowed)
112+
if converter_name not in get_converters():
113+
suffix_converter = _get_format_path_converter(allowed)
114+
register_converter(suffix_converter, converter_name)
109115

110116
suffix_route = '<%s:%s>' % (converter_name, suffix_kwarg)
111117

0 commit comments

Comments
 (0)