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

Custom name for RangeFilter fields #1403

Open
zacharymostowsky opened this issue Jul 28, 2021 · 3 comments
Open

Custom name for RangeFilter fields #1403

zacharymostowsky opened this issue Jul 28, 2021 · 3 comments

Comments

@zacharymostowsky
Copy link

Hello, I would like to use 2 custom field names when using a RangeFilter. Instead of something like myField_max I would want to use something like myFieldMax. This way I can pass the entire params dictionary request.GET directly into the filter while still conforming to the snake case naming convention my front end uses to call my API. Is this currently possible?

Thanks,
Zach

@carltongibson
Copy link
Owner

Hi @zacharymostowsky — this isn’t really do-able at the moment. See the discussion on #302 — where there’s an example.

It would be nice to document the options here. 🤔

@carltongibson
Copy link
Owner

#951 also links to the migration guide for v2 when SuffixedMultiWidget was introduced, but to get myFiledMax you’d need to customise MultiValueField more radically.

(If you do come up with something there, let me know.)

@bufke
Copy link

bufke commented May 21, 2022

Here's the workaround I use. Let's say we want the fields to be called "start" and "end". I've only tested this with DateRange. It still requires setting many classes.

class StartEndDateRangeWidget(DateRangeWidget):
    suffixes = ["start", "end"]  # Set this to the non-suffixed terms you wish

    def suffixed(self, name, suffix):
        return suffix  # Avoids the name_suffix format


class StartEndDateTimeRangeField(DateTimeRangeField):
    widget = StartEndDateRangeWidget


class StartEndDateTimeFromToRangeFilter(filters.DateTimeFromToRangeFilter):
    field_class = StartEndDateTimeRangeField


class FooFilter(FilterSet):
    created = StartEndDateTimeFromToRangeFilter(
        field_name="related__therefore__we__really__cant__use__multiple__fields__created",
        label="Wow so pretty" 
    )

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

No branches or pull requests

3 participants