The Wayback Machine - https://web.archive.org/web/20200603071914/https://github.com/encode/django-rest-framework/issues/7200
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

Problem with SearchFilter and JSONFields #7200

Open
haaavk opened this issue Feb 24, 2020 · 0 comments
Open

Problem with SearchFilter and JSONFields #7200

haaavk opened this issue Feb 24, 2020 · 0 comments

Comments

@haaavk
Copy link

@haaavk haaavk commented Feb 24, 2020

There is a problem with SearchFilter and JSONFields.
When I add json key to search_fields (json_field__key) I got exception:
django.core.exceptions.FieldDoesNotExist: Model has no field named 'field_name'
Exception comes from SearchFilter class and must_call_distinct method.
I made quick fix by overriding must_call_distinct method but it's not a perfect solution.

class JsonSearchFilter(SearchFilter):
    def must_call_distinct(self, queryset, search_fields):
        opts = queryset.model._meta
        search_fields = [
            field
            for field in search_fields
            if not isinstance(opts.get_field(field.split(LOOKUP_SEP)[0]), JSONField)
        ]
        return super().must_call_distinct(queryset, search_fields)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
1 participant
You can’t perform that action at this time.