Skip to content

How to create a filter "not equal" and "not in" #1145

@acrispim

Description

@acrispim

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
        }
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions