question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filtering array query params

See original GitHub issue

Some clients (e.g. Ember) send requests like ?filter[tags][]=1&filter[tags][]=2 for the membership in a list of values (JSON API).

To process this kind of filter I only changed (https://github.com/django-json-api/django-rest-framework-json-api/blob/master/rest_framework_json_api/django_filters/backends.py#L66) from

^filter(?P<ldelim>\[?)(?P<assoc>[\w\.\-]*)(?P<rdelim>\]?$)

to

^filter(?P<ldelim>\[?)(?P<assoc>[\w\.\-]*)(?P<rdelim>\]?)(\[\])?$

Everything seems to work fine. What do you think?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
puremathcommented, Oct 9, 2019

The fact that Ember Data (specifically the JsonAPIAdapter) serializes arrays in the query parameters by adding empty brackets isn’t very well documented. A hint that this is the case can be found here: https://api.emberjs.com/ember-data/3.13/classes/JSONAPIAdapter/methods?anchor=findMany

The IDs will be passed as a URL-encoded Array of IDs, in this form: ids[]=1&ids[]=2&ids[]=3

The relevant code where the actual serialization takes place can be found here: https://github.com/emberjs/data/blob/v3.13.1/packages/adapter/addon/-private/utils/serialize-query-params.ts#L20

Even though this is not detailed out in the JSON API Specification I’d argue to consider the way Ember does this more or less the de-facto standard as there isn’t any other reference client side implementation of the JSON API specification. If not DJA should at least offer the option to handle query parameter lists the Ember way.

0reactions
sliverccommented, Apr 19, 2020

We only want to implement in DJA what is actually JSON API spec specific; otherwise such implementation need to happen in the respective upstream project (Django Filter, DRF or even Django itself). So a allin lookup is not DJA specific in this regard. And how DJA users use DjangoFilterSet is also up to them as JSON API spec doesn’t define how filter arguments are used. If you are referring to changes suggested in https://github.com/django-json-api/django-rest-framework-json-api/issues/718#issuecomment-605821483 I think that it is a good way forward to make the backend more powerful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Array to Query Params for Filters in Searching
You can make a loop through the array and use Object.key() with forEach function to get the property name and value. Share.
Read more >
Array.prototype.filter() - JavaScript - MDN Web Docs
The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given...
Read more >
Filtering on related entries (with an array query param)
I'm getting myself into a potential pickle here and wanting to know the best way to get apply filters on the current scenario:...
Read more >
Filter an array - Algolia
Go to your dashboard and select your index. · Click the “Add Query Parameter” button, which is just below the search bar. ·...
Read more >
An array in Filter Query - Power Platform Community - Microsoft
Hi, I want to filter items using an array variable from another list. The query would be something like this: client eq 'a'...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found