question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use GlobalIDFilter as FilterSet field

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rpkilbycommented, May 23, 2019

I’m not that familiar with graphene-django, but the below is where the filterset is created and called: https://github.com/graphql-python/graphene-django/blob/dc561c68c49d1a91637503f765857d819c36769a/graphene_django/filter/fields.py#L92-L96

Does it make sense for DjangoFilterConnectionField.connection_resolver to raise validation errors? If so, then the code should look like:

filterset = filterset_class(
    data=filter_kwargs,
    queryset=default_manager.get_queryset(),
    request=info.context,
)
if filterset.is_valid():
    return filterset.qs

# I don't know how validation is handled in graphene, but you'd need to convert these 
# validation errors to the equivalent in graphene, and return/raise appropriately.
do_something_with(filterset.errors)


1reaction
phaltcommented, May 21, 2019

I believe this is an issue with django-filter and not graphene-django. Can we close this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to filter a query by a list of ids in GraphQL using ...
GlobalIDMultipleChoiceFilter from django-graphene kinda solves this issue, if you put "in" in the field name. You can create filters like
Read more >
django-filter - Graphene-Python
The filter_fields parameter is used to specify the fields which can be filtered upon. The value specified here is passed directly to django-filter...
Read more >
How to use the graphene-django.graphene_django.filter ...
To help you get started, we've selected a few graphene-django examples, based on popular ways it is used in public projects. Secure your...
Read more >
Passing Parameter from Field Maps to Survey123 INBOX Edit
I am trying to pass an attribute from Field Maps to Survey123, ... the globalId parameter as well (globalId=, filter only filters the...
Read more >
Tips and Solutions — django-filter 22.1 documentation
You should use filters that match the data type of the lookup expression instead of the data type underlying the model field. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found