You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Parameter inspector for DRF views is ignoring the format of lookup described in overriden widget.
For example I've overrided the RangeFilter (django_filters.widgets.RangeWidget) so it started using __ (double underscore) as a separator for field_name and suffixes (min/max).
Generally it reproduces with any filters that are using widgets inherited from django_filters.widgets.SuffixedMultiWidget
The issues is that filters are parsed properly from received request with double underscore passed as separator, but API schema is wrong. Schema still generating with single underscore separator
I've debuged this issue and discovered issue inside drf_spectacular.contrib.django_filters.py:DjangoFilterExtension.resolve_filter_field method. Here is permalink.
I think it's better to use widget().suffixed() method here, instead hardcoded separator
Dependencies versions:
Django==5.0.4
djangorestframework==3.15.1
django-filter==24.2
drf-spectacular==0.27.2
To Reproduce
Set up filterset to use custom Filter, field_class and widget. Here is short sample of custom changes
# models.pyfromdjango.dbimportmodelsclassSomeModel(models.Model):
name=models.CharField(max_length=100)
some_age=models.PositiveSmallIntegerField()
# filterset.pyfromdjango_filtersimportrest_frameworkasfiltersfromdjango_filtersimportfields, widgetsclassCustomRangeWidget(widgets.RangeWidget):
"""override RangeWidget to use custom separator"""defsuffixed(self, name, suffix):
separator="__"# my custom separator overridereturnseparator.join([name, suffix]) ifsuffixelsenameclassCustomRangeField(fields.RangeField):
"""Override RangeField to use custom widget"""widget=CustomRangeWidgetclassCustomNumericRangeFilter(filters.RangeFilter):
"""Override RangeFilter to use custom field_class"""field_class=CustomRangeFieldclassPublicationFilterSet(filters.FilterSet):
some_age=CustomNumericRangeFilter
Connect such filterset to your model view
Check the generated schema
Expected behavior
I expect that shema should be a bit more dynamicly generated and use existing functionality for building field_names on RangeFilters.
Here is my suggestion how to do it
DoubleTroubleOf
changed the title
API Schema generating wrong is SuffixedMultiWidget overriden
API Schema generating wrong if SuffixedMultiWidget overriden
Dec 11, 2024
Describe the bug
Parameter inspector for DRF views is ignoring the format of lookup described in overriden widget.
For example I've overrided the RangeFilter (
django_filters.widgets.RangeWidget
) so it started using__
(double underscore) as a separator forfield_name
andsuffixes
(min/max).Generally it reproduces with any filters that are using widgets inherited from
django_filters.widgets.SuffixedMultiWidget
The issues is that filters are parsed properly from received request with double underscore passed as separator, but API schema is wrong. Schema still generating with single underscore separator
I've debuged this issue and discovered issue inside
drf_spectacular.contrib.django_filters.py:DjangoFilterExtension.resolve_filter_field
method. Here is permalink.I think it's better to use
widget().suffixed()
method here, instead hardcoded separatorDependencies versions:
To Reproduce
Expected behavior
I expect that shema should be a bit more dynamicly generated and use existing functionality for building field_names on RangeFilters.
Here is my suggestion how to do it
instead
The text was updated successfully, but these errors were encountered: