Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Converter is already registered" deprecation warning. #9512

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions rest_framework/urlpatterns.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.urls import URLResolver, include, path, re_path, register_converter
from django.urls.converters import get_converters
from django.urls.resolvers import RoutePattern

from rest_framework.settings import api_settings


def _get_format_path_converter(suffix_kwarg, allowed):
def _get_format_path_converter(allowed):
if allowed:
if len(allowed) == 1:
allowed_pattern = allowed[0]
Expand All @@ -23,11 +24,14 @@ def to_python(self, value):
def to_url(self, value):
return '.' + value + '/'

return FormatSuffixConverter


def _generate_converter_name(allowed):
converter_name = 'drf_format_suffix'
if allowed:
converter_name += '_' + '_'.join(allowed)

return converter_name, FormatSuffixConverter
return converter_name


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

converter_name, suffix_converter = _get_format_path_converter(suffix_kwarg, allowed)
register_converter(suffix_converter, converter_name)
converter_name = _generate_converter_name(allowed)
if converter_name not in get_converters():
suffix_converter = _get_format_path_converter(allowed)
register_converter(suffix_converter, converter_name)

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

Expand Down
Loading