Reputation: 906
I have the following model in Django:
class JobPost(models.Model):
company = models.CharField(blank=True, max_length=30, null=True)
job = models.CharField(blank=True, max_length=30, null=True)
category = models.CharField(blank=True, max_length=30, null=True)
description = models.TextField(blank=True, max_length=500, null=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.job
And I have the following schema with graphene:
class JobPostNode(DjangoObjectType):
class Meta:
# Assume you have an Animal model defined with the following fields
model = JobPost
filter_fields = {
'company': ['exact', 'icontains', 'istartswith'],
'job': ['exact', 'icontains', 'istartswith'],
'category': ['exact', 'icontains', 'istartswith'],
"description": ['exact', 'icontains', 'istartswith'],
}
interfaces = (relay.Node,)
class Query(graphene.ObjectType):
job = relay.Node.Field(JobPostNode)
all_jobs = DjangoFilterConnectionField(JobPostNode)
schema = graphene.Schema(query=Query)
I want to use icontains
whereas I will get data based on an OR not on AND; for example the following query:
{
allJobs(job_Icontains: "t", company_Icontains: "v") {
edges {
node {
company
job
}
}
}
}
Should return data that has the letter "t" in job OR the letter "v" in company, not the letter "t" in job AND the letter "v" in company. How can I do that?
Upvotes: 0
Views: 1128
Reputation: 906
I found out how to do this here! For my schema I wrote this:
import graphene
from api_rest.models import JobPost
from django.db.models import Q
from graphene_django import DjangoObjectType
class JobType(DjangoObjectType):
class Meta:
model = JobPost
class Query(graphene.ObjectType):
jobs = graphene.List(
JobType, search=graphene.String(), first=graphene.Int(), skip=graphene.Int()
)
def resolve_jobs(self, info, search=None, first=None, skip=None, **kwargs):
queryset = JobPost.objects.all()
if search:
filter = (
Q(company__icontains=search)
| Q(description__icontains=search)
| Q(category__icontains=search)
)
queryset = queryset.filter(filter)
if skip:
queryset = queryset[skip:]
if first:
queryset = queryset[:first]
return queryset
schema = graphene.Schema(query=Query)
Now, using the following query, I am getting the results I want:
{
jobs(search: "transpo") {
id
company
category
job
createdAt
}
}
Upvotes: 1