-
Hello, 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.
Answered by
tcleonard
Mar 18, 2021
Replies: 2 comments
-
Note that in object = DjangoFilterConnectionField(ObjectType) To achieve what you want you need to declare a filter in a 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.
0 replies
Answer selected by
zbyte64
-
It worked perfectly, thank you very much for the light! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that in
result/types.py
you should not need to do:To achieve what you want you need to declare a filter in a
filterset_class
like so: