-
Notifications
You must be signed in to change notification settings - Fork 766
-
Hello,
I tried to find solutions on how to carry out not equal
and not in
filtering, but due to my inexperience I was unable to identify a solution.
this is a fragment of my current structure.
# object/models.py class Object(models.Model): ... object_name = models.CharField("Nome do Objeto", max_length=30, default='') ... # object/type.py class ObjectType(DjangoObjectType): class Meta: model = Object filter_fields = { 'object_name': ['exact', 'icontains'], ... } convert_choices_to_enum = False interfaces = (graphene.relay.Node,) # object/queries.py class Query(object): objects = graphene.relay.Node.Field(ObjectType) all_objects = DjangoFilterConnectionField(ObjectType) # result/models.py class Result(models.Model): ... object = models.ForeignKey(Object, on_delete=models.PROTECT) ... # result/types.py class ResultType(DjangoObjectType): class Meta: model = Result filter_fields = { ... 'object__object_name': ['exact', 'icontains'], ... } convert_choices_to_enum = False interfaces = (graphene.relay.Node,) object = DjangoFilterConnectionField(ObjectType) # result/queries.py class Query(object): results = DjangoFilterConnectionField(ResultType)
I wanted to be able to perform searches similar to:
# result/types.py class ResultType(DjangoObjectType): class Meta: model = Result filter_fields = { ... 'object__object_name': ['exact', 'icontains', 'notequal', 'notin'], ... } convert_choices_to_enum = False interfaces = (graphene.relay.Node,) object = DjangoFilterConnectionField(ObjectType)
query { results ( object_ObjectName__Notequal: "Pluto"){ edges { node { object { objectName } } } } }
query { results ( object_ObjectName__Notin: ["Pluto", "Quaoar"]){ edges { node { object { objectName } } } } }
Beta Was this translation helpful? Give feedback.
All reactions
Note that in result/types.py
you should not need to do:
object = DjangoFilterConnectionField(ObjectType)
To achieve what you want you need to declare a filter in a filterset_class
like so:
from django_filters import Filter, FilterSet from graphene_django.filter import ListFilter class ResultFilterSet(FilterSet): class Meta: model = Result fields = { "object__object_name": ["exact", "icontains"], } object__object_name_notequal = Filter(field_name="object__object_name", lookup_expr="exact", exclude=True) object__object_name_notin = ListFilter(field_name="object__object_name", lookup_expr="in", exclude=True) class ResultType(DjangoObjectType)...
Replies: 2 comments
-
Note that in result/types.py
you should not need to do:
object = DjangoFilterConnectionField(ObjectType)
To achieve what you want you need to declare a filter in a filterset_class
like so:
from django_filters import Filter, FilterSet from graphene_django.filter import ListFilter class ResultFilterSet(FilterSet): class Meta: model = Result fields = { "object__object_name": ["exact", "icontains"], } object__object_name_notequal = Filter(field_name="object__object_name", lookup_expr="exact", exclude=True) object__object_name_notin = ListFilter(field_name="object__object_name", lookup_expr="in", exclude=True) class ResultType(DjangoObjectType): class Meta: model = Result filterset_class = ResultFilterSet convert_choices_to_enum = False interfaces = (graphene.relay.Node,)
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1 -
🎉 1 -
🚀 1
-
It worked perfectly, thank you very much for the light!
Beta Was this translation helpful? Give feedback.