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

QueryArrayWidget does not work with the foo=bar,baz syntax #1685

Open
gabn88 opened this issue Sep 6, 2024 · 4 comments
Open

QueryArrayWidget does not work with the foo=bar,baz syntax #1685

gabn88 opened this issue Sep 6, 2024 · 4 comments

Comments

@gabn88
Copy link

gabn88 commented Sep 6, 2024

According to the docs (

class QueryArrayWidget(BaseCSVWidget, forms.TextInput):
) the QueryArrayWidget should work with the foo=bar,baz syntax. It does however test for this syntax by testing for MultiValueDict.

The problem here is that a QueryDict (the actual data type) is a subclass of MultiValueDict and the result always returns False and the data never get into the if statement.

I fixed it by replacing the code with:

        if not type(data) == MultiValueDict:
            data = data.copy()
            ret = {}
            for key, value in data.items():
                if type(value) == list:
                    value = list[0]
                # treat value as csv string: ?foo=1,2
                if isinstance(value, str):
                    ret[key] = [x.strip() for x in value.rstrip(",").split(",") if x]
            data = MultiValueDict(ret)

But I'm sure there must be a better fix.

@carltongibson
Copy link
Owner

I'm not sure I'm following. 🤔

The query array widget is for query strings using the old PHP format, ?foo[]=bar&foo[]=baz, not CSV input.

Can you clarify what you're trying to do here?

@gabn88
Copy link
Author

gabn88 commented Sep 6, 2024 via email

@carltongibson
Copy link
Owner

Ok, so the first port of call is a failing test case demonstrating the issue, and then we can look at whether the fix is optimal, or if we can find a better one.

Would you like to make a PR?

@gabn88
Copy link
Author

gabn88 commented Sep 6, 2024

See here:
#1686

First commit shows the failing test
Second commit shows the solution.

As a side note: how can the tests be so fast? Does it skip database tests?

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