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

Field lists in query parameters such as DRF's ordering filter #87

Open
nimame opened this issue Oct 19, 2020 · 1 comment
Open

Field lists in query parameters such as DRF's ordering filter #87

nimame opened this issue Oct 19, 2020 · 1 comment

Comments

@nimame
Copy link

nimame commented Oct 19, 2020

When using rest_framework.filters.OrderingFilter in a view, one can add a list of comma-separated field names to an "order" query parameter (see the docs). However, those lists are currently not affected by this package, making it incompatible with it out-of-the-box. I solved this, by extending the get_ordering method of the OrderingFilter class:

from rest_framework.filters import OrderingFilter
from djangorestframework_camel_case.util import camel_to_underscore
from djangorestframework_camel_case.settings import api_settings


class CamelCaseOrderingFilter(OrderingFilter):

    def get_ordering(self, request, queryset, view):

        params = request.query_params.get(self.ordering_param)
        if params:
            fields = [camel_to_underscore(field.strip(), **api_settings.JSON_UNDERSCOREIZE,)
                      for field in params.split(',')]
            ordering = self.remove_invalid_fields(queryset, fields, view, request)
            if ordering:
                return ordering

        return self.get_default_ordering(view)

Now there are other packages using field lists in query parameters. I am currently trying to add drf-flex-fields to my app and I haven't found a good way to customize it to use "underscoreize" the fields and expand lists in this case.

Has anyone else faced this problem and has a solution? Or is it maybe in the scope of this project to provide a solution for field lists in query parameters?

@mrudelle
Copy link

For anyone looking for adding support for the browsable api or other package that depend on get_valid_fields:

class CamelCaseOrderingFilter(OrderingFilter):

    def get_ordering(self, request, queryset, view):

        ordering = super().get_ordering(request, queryset, view)

        if ordering is None:
            return None

        return [
            camel_to_underscore(field, **api_settings.JSON_UNDERSCOREIZE)
            for field in ordering
        ]

    def get_valid_fields(self, queryset, view, context=None):
        if context is None:
            context = {}
        fields = super().get_valid_fields(queryset, view, context=context)

        return [
            (re.sub(camelize_re, underscore_to_camel, f[0]), f[1])
            for f in fields
        ]

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

2 participants