graphene-django-extras
graphene-django-extras copied to clipboard
How can you apply a global filter to the default queryset of a type?
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?
Having similar issue. Also tracking similar question on graphene repo: https://github.com/graphql-python/graphene/issues/1198
Hitting a similar issue, but trying to make a queryset distinct. Did you have any luck?