Skip to content

Problem with SearchFilter and JSONFields #7200

@haaavk

Description

@haaavk

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions