graphene-django-extras icon indicating copy to clipboard operation
graphene-django-extras copied to clipboard

How can you apply a global filter to the default queryset of a type?

Open honi opened this issue 5 years ago • 2 comments

I want to be able to apply a global filter for a model, for example by a published field.

See this example code and the comment inside the get_queryset method:

class Category(models.Model):
    name = models.CharField(max_length=128)
    published = models.BooleanField(default=True)


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category


class CategoryType(DjangoSerializerType):
    class Meta:
        serializer_class = CategorySerializer
        pagination = LimitOffsetGraphqlPagination(default_limit=2, ordering='name')

    @classmethod
    def get_queryset(cls, queryset, info):
        # This is one of graphene's documented approaches but it seems this library does not call this method.
        return queryset.filter(published=True)


class Query(graphene.ObjectType):
    category, categories = CategoryType.QueryFields()

What would be the recommended approach to achieve this using this library?

honi avatar Oct 17 '20 15:10 honi

Having similar issue. Also tracking similar question on graphene repo: https://github.com/graphql-python/graphene/issues/1198

keithhackbarth avatar Feb 06 '21 01:02 keithhackbarth

Hitting a similar issue, but trying to make a queryset distinct. Did you have any luck?

paulsermon-gemfair avatar Aug 19 '22 16:08 paulsermon-gemfair