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

Cannot filter on jsonfields (django) #21

Open
ohenrik opened this issue Sep 12, 2016 · 6 comments
Open

Cannot filter on jsonfields (django) #21

ohenrik opened this issue Sep 12, 2016 · 6 comments

Comments

@ohenrik
Copy link

ohenrik commented Sep 12, 2016

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 ohenrik changed the title Cannot filter on jsonfields Cannot filter on jsonfields (django) Sep 12, 2016
@miki725
Copy link
Owner

miki725 commented Jan 26, 2017

@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?

@selimt
Copy link

selimt commented Oct 17, 2018

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

@miki725
Copy link
Owner

miki725 commented Oct 18, 2018

currently json fields are not supported. sorry.

@prarabdh9909
Copy link

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?

@miki725
Copy link
Owner

miki725 commented Jan 24, 2019

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.

@sjkingo
Copy link

sjkingo commented Apr 23, 2019

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

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

5 participants