Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
honi opened this issue Oct 17, 2020 · 2 comments
Open

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

honi opened this issue Oct 17, 2020 · 2 comments

Comments

@honi
Copy link

honi commented Oct 17, 2020

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
Copy link
Contributor

keithhackbarth commented Feb 6, 2021

Having similar issue. Also tracking similar question on graphene repo: graphql-python/graphene-django#1526

@paulsermon-gemfair
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants