django-url-filter icon indicating copy to clipboard operation
django-url-filter copied to clipboard

Cannot filter on jsonfields (django)

Open ohenrik opened this issue 9 years ago • 6 comments
trafficstars

I'm trying to filter on a jsonfield in django named data_filters. However it seems like this is not working when I'm not specifying any filters. And when i try to spesify filters it crashes because the same fields are not present everywhere.

Is there a way to not validate filters? So that i can just pass along any filter value? for example ?data_filters__region__id=01,02

Edit: Can i use the plain filter to acchieve this?

Also it seems like the pip install version of this gem (0.2.0) does not include the plain version of the filter, it seems like i have to request the master branch to get this.

ohenrik avatar Sep 12 '16 10:09 ohenrik

@ohenrik sorry for the late response

unfortunately currently thats not possible since different filtersets cant be mixed and matched. this is a great idea though. would be nice to be able to do something like:

class DataFiltersFilterSet(PlainModelFilterSet):
    class Meta:
        model = {
            'my': 'json'
        }

class MyModelFilterSet(ModelFilterSet):
    data_filters = DataFiltersFilterSet()
    class Meta:
        model = MyModel
        fields = ['id', ...]

Issue now is to filter queryset, only a single backend is used. since Django model backend would be unable to filter plain objects, it would not be able to filter on json object data. And vice-versa for plain backend would not be able to filter Django models.

Can you explain a bit more about your usage example so maybe there would be temporary solution for you?

miki725 avatar Jan 26 '17 21:01 miki725

Hi, revisiting this issue since we are running into the problem as well. Currently if a jsonfield contains a dictionary we are unable to filter for a value for a key in that dictionary. For example:

class TransferRequest(models.Model):
   ...
   options = JSONField(default=dict, blank=True)
   ...

class TransferRequestViewSet(QueryOrderableViewSetMixin, viewsets.ModelViewSet):
    queryset = TransferRequest.objects.all()
    serializer_class = TransferRequestSerializer
    filter_backends = [DjangoFilterBackend]
    filter_fields = [..., 'options', ...]

And we have the following data for the options:

{"backend": "rsync"}

We can not write our query like the following:

http://website/api/requests/?options__backend="rsync"

but in Django we can write it as follows

TransferRequest.objects.filter(options__backend="rsync")

I guess django_url_filter thinks "options" is a related model.

Is therea way to get this to work with the current implementation?

Thanks -Selim

selimt avatar Oct 17 '18 19:10 selimt

currently json fields are not supported. sorry.

miki725 avatar Oct 18 '18 00:10 miki725

Hi @miki725 , This project has been really helpful for me.

I would like to contribute and take up this task to add json field support. Are there guidelines on how do I become a contributor?

prarabdh9909 avatar Jan 24 '19 06:01 prarabdh9909

no specific guidelines. the complexity with json fields is that it will require some structure changes to the library which im not sure how to proceed with yet. I have some ideas brewing in my head. when will have some free time (or if will actually require them in personal/work project 😄 ) will experiment what works better. there are couple of aspects which I dont particularly enjoy at the moment like lookups are not restricted per field type. fixing that pattern with additional abstraction layer below backend can potentially help with json fields too. fee free to try get something running for json fields and send pr although no guarantees it will be merged but could be great to start a discussion on how to approach the solution.

miki725 avatar Jan 24 '19 13:01 miki725

I have made a PR #82 that adds support for filtering against JSONField()'s

sjkingo avatar Apr 23 '19 05:04 sjkingo