eamigo86 / graphene-django-extras

Extras functionalities for Graphene-Django
MIT License
417 stars 107 forks source link

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

Open honi opened 4 years ago

honi commented 4 years ago

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?

keithhackbarth commented 3 years ago

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

paulsermon-gemfair commented 2 years ago

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