简体   繁体   中英

graphene-django returns wrong filtered data

I have a DjangoObjectType and a Filterset class for my query. I have added a filter for my DjangoObjectType class as below.

class MyFilter(FilterSet):
    quantity = NumberFilter(method='filter_quantity')

    def filter_quantity(self, queryset, name, value):
        a_data = ClassA.objects.filter(quantity__gte=value)
        a_data_keys = [data.key_number for data in a_data]
        return queryset.filter(key__in=a_data_keys)

When I run my query without any filter, it works correctly and returns all the data. However, when I run it eg with the quantity: 1 filter, it returns data with just quantity: 1 , same as for 2. It doesn't return equal or greater . I have some data with quantity: 3 . When I run the query with this filter and 3, it returns just quantity: 4 . I'm really confused what's wrong with this. When I print the query filter result before return, it seems correct. But in the graphql query result dict, it's wrong. Do you have any idea about how can I fix this weird issue?

It's hard to say because this is a limited snippet of the code involved, and this doesn't look wrong. Could it be possible that this filter is not being applied by graphene? are you using a DjangoFilterConnection field? Maybe you need a resolve method as follows?

class Query(graphene.ObjectType):
    filtered_stuff = DjangoFilterConnectionField(StuffType)

    def resolve_filtered_stuff(self, info, **filters):
        return MyFilter(filters).qs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM