Skip to content

Can't use GlobalIDFilter as FilterSet field #542

@angaz

Description

@angaz

I noticed that when I was trying to use a GlobalIDFilter as a FilterSet field, it doesn't call the filter method (both my custom method filter_table_id and GlobalIDFilter.filter is not called) and returns all of the records in the table. If I use a CharFilter instead of a GlobalIDFilter, it works and filters as expected.

This works. But type is String in queries.

class ModelFilter(FilterSet):
    def filter_table_id(self, queryset, name, value):
        _, _id = from_global_id(value)
        return queryset.filter(table1__table2__table3__id=_id)

    table_id = CharFilter(method='filter_table_id')

    class Meta:
        model = DBModel
        fields = {
            'name': ['exact', 'icontains'],
            'table_id': ['exact'],
        }

But if I replace the CharFilter with GlobalIDFilter, the type is ID in the query, but all the records are returned, none are filtered out.

class ModelFilter(FilterSet):
    def filter_table_id(self, queryset, name, value):
        _, _id = from_global_id(value)
        return queryset.filter(table1__table2__table3__id=_id)

    table_id = GlobalIDFilter(method='filter_table_id')

    class Meta:
        model = DBModel
        fields = {
            'name': ['exact', 'icontains'],
            'table_id': ['exact'],
        }

I tried to see why it was not working, but I could not find out why the filter function nor my filter_table_id was being called.
Am I using the wrong thing for filtering?
Is there a better way to do what I want?

I am using venv with:

  • Python 3.7.0 (default, Sep 12 2018, 18:30:08), the default Python3.7 Ubuntu 18.04 package.
  • graphene-django==2.2.0
  • django-filter==2.0.0
  • graphene==2.1.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions