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

Unable to apply generic filters #38

Open
thclark opened this issue Aug 30, 2019 · 0 comments
Open

Unable to apply generic filters #38

thclark opened this issue Aug 30, 2019 · 0 comments

Comments

@thclark
Copy link

thclark commented Aug 30, 2019

Using generic filtering on the rest framework (with the common default filter 'django_filters.rest_framework.DjangoFilterBackend') does not work, because we cannot set the filterset_fields attribute when generating the views.

For example, the following fails when filtering by a 'layer' field on my model:

# urls.py
...
urlpatterns = [
    url(r'^locations/$',
        generics.GeoListView.as_view(
            queryset=Location.objects.all(),
            filterset_fields=('layer',)
        ),
        name='location-list'),
]

The error raised is: TypeError: GeoListView() received an invalid keyword 'filterset_fields'. as_view only accepts arguments that are already attributes of the class.

This isn't strictly an issue with spillway, since django-filter isn't in the rest framework core... the docs clearly show that a view should be inherited from generic, and the filterset_fileds attribute added within your own view class.

Thus, this issue can be worked around by adding the following views.py:

from spillway import generics

class GeoDetailView(generics.GeoDetailView):
    """Generic detail view providing vector geometry representations."""
    filterset_fields = ()

class GeoListView(generics.GeoListView):
    """Generic view for listing a geoqueryset."""
    filterset_fields = ()

class GeoListCreateAPIView(generics.GeoListCreateAPIView):
    """Generic view for listing or creating geomodel instances."""
    filterset_fields = ()

class RasterDetailView(generics.RasterDetailView):
    """View providing access to a Raster model instance."""
    filterset_fields = ()

class RasterListView(generics.RasterListView):
    """View providing access to a Raster model QuerySet."""
    filterset_fields = ()

which allows quick declaration of the API endpoints in urls.py as at the top of this issue.

That's the "right" way of doing it; so isn't a bug with spillway, but it means you have to have quite a bit of boilerplate.

Suggestion / Quick fix
I'd recommend adding:

    filterset_fields = ()

as an attribute in BaseGeoView and BaseRasterView from generics.py so that the users can really quickly and easily use your example without then having to refactor to get filtering working when they hit this problem.

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant