Skip to content

Generate URL Filter-squashed#2882

Open
samialfattani wants to merge 2 commits into
pallets-eco:masterfrom
samialfattani:url-filter11
Open

Generate URL Filter-squashed#2882
samialfattani wants to merge 2 commits into
pallets-eco:masterfrom
samialfattani:url-filter11

Conversation

@samialfattani
Copy link
Copy Markdown
Contributor

@samialfattani samialfattani commented Apr 27, 2026

Fixes #2703

This PR adds a function to generate a URL with filter arguements including all types of filters (Like, NotLike, Greater....etc) and including both indexed (flt2_3=xx) and named (flt3_email_like=@example.com)

Usage:

    filtered_url = view.url_for(
        search="exam",
        filters=[
            filters.FilterLike(Model1.test1, "Email", url_value="@example.com"),
            filters.BooleanEqualFilter(Model1.bool_field, "active", url_value=False),
            filters.FloatGreaterFilter(Model1.test5, "Salary", url_value=15.3),
        ],
    )

Output:
/admin/user/?search=exam&flt0_0=@example.com&flt1_21=0&flt2_16=15.3

Output with named parameters:
/admin/user/?search=exam&flt0_test1_contains=@example.com&flt1_bool_field_equals=0&flt4_test2_greater_than=15.3

@samialfattani samialfattani changed the title Generate URL Filter Generate URL Filter-squashed Apr 27, 2026
Copy link
Copy Markdown
Contributor

@samuelhwilliams samuelhwilliams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my main issue here is on the implementation being that the URL value is baked into the Filter definition. I'm not sure this feels like the right developer experience/API.

I think we should take this back to an issue and plan out the implementation up front rather than do it on a PR.

I think I'd be more interested in something like this:

    filtered_url = view.url_for(
        search="exam",
        filters=[
            (filters.FilterLike(Model1.test1, "Email"), "@example.com"),
            (filters.BooleanEqualFilter(Model1.bool_field, "active"), False),
            (filters.FloatGreaterFilter(Model1.test5, "Salary", 15.3),
        ],
    )

Ideally with full type support so that type checkers can statically verify that you've passed the correct argument type for the appropriate filter, ie boolean values for a Boolean filter and string values for a like filter.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to this example aren't needed as part of this PR - ideally we would keep PRs small and scoped to help review. This could have gone in separately if it's just for general example behaviour unrelated to generating URL filters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simply i wanted to add some records to test the filter's js behavior. as you know filters input fields are fully created by JS and there is no way to test them using pytest. I think changes in this example accelrate testing the filter.

Comment thread examples/bootstrap4/files/d1/dummy3.js
Comment thread flask_admin/contrib/mongoengine/filters.py Outdated
Comment thread flask_admin/model/base.py Outdated
Comment thread flask_admin/model/filters.py Outdated
Comment thread flask_admin/model/base.py Outdated
@samialfattani
Copy link
Copy Markdown
Contributor Author

now this style is adapted. you can check and review

    filtered_url = view.url_for(
        search="exam",
        filters=[
            (filters.FilterLike(Model1.test1, "Email"), "@example.com"),
            (filters.BooleanEqualFilter(Model1.bool_field, "active"), False),
            (filters.FloatGreaterFilter(Model1.test5, "Salary", 15.3),
        ],
    )

commit 3e99002e0e9dc46a0eac40961950490e7387bc42
Merge: e2e0092 c8ac222
Author: Sami Alfattani <Sami_alfattani@hotmail.com>
Date:   Sun May 24 10:12:20 2026 +0300

    Merge branch 'master' into url-filter11

commit e2e0092
Author: Sami Alfattani <Sami_alfattani@hotmail.com>
Date:   Sat May 16 08:07:07 2026 +0300

    refactor: remove url_value parameter from filter classes and update related code

commit ab022e1
Author: Sami Alfattani <Sami_alfattani@hotmail.com>
Date:   Wed May 13 22:18:38 2026 +0300

    fix typing

commit a864bb7
Merge: 51c81b7 23be1ac
Author: Sami Alfattani <Sami_alfattani@hotmail.com>
Date:   Wed May 13 21:35:37 2026 +0300

    Merge branch 'master' into url-filter11

commit 51c81b7
Author: Sami Alfattani <Sami_alfattani@hotmail.com>
Date:   Wed May 13 21:11:11 2026 +0300

    fix conflicts

commit 8cdccec
Author: Sami Alfattani <Sami_alfattani@hotmail.com>
Date:   Mon Apr 27 21:56:56 2026 +0300

    Generate URL Filter

    This PR adds a function to generate a URL with filter arguements including all types of filters (Like, NotLike, Greater....etc) and including both indexed (`flt2_3=xx`) and named (`flt3_email_like=@example.com`)

    **Usage:**
    ```
        filtered_url = view.url_for(
            search="exam",
            filters=[
                filters.FilterLike(Model1.test1, "Email", url_value="@example.com"),
                filters.BooleanEqualFilter(Model1.bool_field, "active", url_value=False),
                filters.FloatGreaterFilter(Model1.test5, "Salary", url_value=15.3),
            ],
        )
    ```

    **Output:**
    `/admin/user/?search=exam&flt0_0=@example.com&flt1_21=0&flt2_16=15.3`

    **OR with named parameters:**
    `/admin/user/?search=exam&flt0_test1_contains=@example.com&flt1_bool_field_equals=0&flt4_test2_greater_than=15.3`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Add method to easily generate a pre-filtered URL

2 participants