Skip to content

Custom django path's param convert type feature #502

@jtamm-red

Description

@jtamm-red

Custom param convert is very useful. Example it is possible to convert from string to date.

I tried to use decorator extend_schema_field. It is not working.

from drf_spectacular.utils import extend_schema_field

@extend_schema_field({'type': 'integer'})
class NegativeIntConverter:
    regex = '-\d+'

    def to_python(self, value) -> int:
        return int(value)

    def to_url(self, value):
        return '%d' % value


from django.urls import path, include, register_converter
register_converter(NegativeIntConverter, 'negint')

urlpatterns = [
    path('calc/<negint:value>', get_calc)
]

I received warning log:
could not derive type of path parameter "value" because because it is untyped and obtaining queryset from the viewset failed. Consider adding a type to the path (e.g. <int:value>) or annotating the parameter type with @extend_schema. Defaulting to "string".

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfix confirmation pendingissue has been fixed and confirmation from issue reporter is pending

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions