Closed atkawa7 closed 7 years ago
Hi,
The discussion group is the best place to take this discussion and other usage questions. Thanks!
@xordoquy Isn't this a real issue? I have been facing the same problem, from v3.5.4 to v3.6.2.
One similarity of our use cases is that rest_framework.filters.DjangoFilterBackend
and rest_framework.filters.OrderingFilter
is used together.
The browsable api interface only shows ordering selections, without filtering forms!
class DrugEventListView(ListAPIView):
permission_classes = (permissions.IsAuthenticated,)
queryset = DrugEvent.objects.all()
serializer_class = DrugEventSerializer
filter_class = DrugEventFilter
filter_backends = (OrderingFilter, DjangoFilterBackend)
ordering_fields = ('date',)
@wonderbeyond Not impossible that there's an actual issue here. Something like before and after screenshots might help highlight if there's something we need to look into further or not.
I tested it (against latest stable) and it works both with ordering and filtering. You may want to narrow the issue to a simple test case in order to make sure it's not some sort of side effect of another piece of code.
I tried again, the found a more specific behavior.
This case I only used DjangoFilterBackend
, without OrderingFilter
, But still set view.filter_backends
explicitly, Then the similar issue arose, I just cant see the Filters button.
(I've set DEFAULT_FILTER_BACKENDS
to be DjangoFilterBackend
in settings. So I normally dont set view.filter_backends
)
class ListRetrieveDrugViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Drug.objects.all()
# filter_backends = (DjangoFilterBackend,)
filter_class = DrugFilter
class ListRetrieveDrugViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Drug.objects.all()
filter_backends = (DjangoFilterBackend,)
filter_class = DrugFilter
Thanks! Let's reopen this for further investigation.
@wonderbeyond are you sure you have django crispy forms installed ?
Ah, on second thoughts...
I tested it (against latest stable) and it works both with ordering and filtering
Okay, so would be happy to re-open this, but a test case or simplest possible instructions to replicate would be needed first. Thanks @xordoquy!
For the record, here's what has been tested working with Django 1.11, DRF 3.6.2 and django filter 1.0.2:
class CompanyFilter(filters.FilterSet):
class Meta:
model = models.Company
fields = ['name', 'siret']
class Company(viewsets.ModelViewSet):
queryset = models.Company.objects.all()
serializer_class = serializers.Company
filter_backends = [DjangoFilterBackend]
filter_class = CompanyFilter
@xordoquy Yes, I have crispy installed, but I have removed it from installed apps during latest test.
I'll try a minimal demo for reproducing tomorrow...
@xordoquy Hi, I've done my minimal demo project for reproducing this issue, without any magic. https://github.com/wonderbeyond/drf-issue4689
views.py:
from rest_framework.filters import DjangoFilterBackend
vs:
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend',
),
I don't think we'll fix the rest_framework.filters.DjangoFilterBackend
though.
@wonderbeyond thank you a lot for the work on this.
The issue comes from the rest_framework.filters.DjangoFilterBackend
that doesn't show the filter box while django_filters.rest_framework.DjangoFilterBackend
does (even when defined in the view itself).
@xordoquy That's really a big confusion.
rest_framework.filters.DjangoFilterBackend
does its work but not show the filtering ui.
I even didn't notice the two different filter classes when involved into this issue.
Yup, that wasn't obvious. I'll also make a pass on the documentation to remove some misleading code examples. Most are correct but not all.
This comes up because of the original django_filters.FilterSet
is not optimised for DRF — it's designed for (vanilla) view usage.
The newer django_filters.rest_framework.FilterSet
— which is used by the newer django_filters.rest_framework.DjangoFilterBackend
has several optimisations for API usage.
The naming confusion is unfortunate but just a consequence of migrating the filtering code to Django Filter.
It's worth reviewing the Django Filter Docs on Integration with DRF here.
Searching the project, the string rest_framework.filters.DjangoFilterBackend
occurs just once:
warnings.warn(
"The built in 'rest_framework.filters.DjangoFilterBackend' is deprecated. "
"You should use 'django_filters.rest_framework.DjangoFilterBackend' instead.",
DeprecationWarning
)
The docs examples have all been updated.
This was ambiguous: 6831472a6237eaf8f513f0af333da86760ef7871
For the record, here's what has been tested working with Django 1.11, DRF 3.6.2 and django filter 1.0.2:
class CompanyFilter(filters.FilterSet): class Meta: model = models.Company fields = ['name', 'siret'] class Company(viewsets.ModelViewSet): queryset = models.Company.objects.all() serializer_class = serializers.Company filter_backends = [DjangoFilterBackend] filter_class = CompanyFilter
I'm missing
filter_class
so it doesn't showFilter
feature. And you also usingfilterset_fields
below:
class ProductList(generics.ListAPIView):
queryset = Product.objects.all()
serializer_class = ProductSerializer
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('category', 'in_stock')
Refer docs
Now it's work fine for me on environment:
python_version="3.6"
django="2.1"
Thanks @xordoquy ,
For anyone still experiencing this.
"filter_fields" has been depricated.
You have to change it to "filterset_fields" like @xordoquy is doing in his example 😊
django rest filter working but browsable api not showing filter forms. Here is my code
I have django crispy forms installed as well