如何使用 OR 条件在多个字段中使用 graphene-django 的 icontains
How to use OR condition for icontains in multiple fields with graphene-django
我在 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
我有以下石墨烯模式:
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)
我想使用 icontains
而我将根据 OR 而不是 AND 获取数据;例如以下查询:
{
allJobs(job_Icontains: "t", company_Icontains: "v") {
edges {
node {
company
job
}
}
}
}
应该return 工作中有字母“t”或公司中有字母“v”的数据,而不是工作中有字母“t”和公司中有字母“v”的数据。我该怎么做?
我找到了如何做到这一点 here!对于我的架构,我这样写:
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)
现在,使用以下查询,我得到了我想要的结果:
{
jobs(search: "transpo") {
id
company
category
job
createdAt
}
}
我在 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
我有以下石墨烯模式:
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)
我想使用 icontains
而我将根据 OR 而不是 AND 获取数据;例如以下查询:
{
allJobs(job_Icontains: "t", company_Icontains: "v") {
edges {
node {
company
job
}
}
}
}
应该return 工作中有字母“t”或公司中有字母“v”的数据,而不是工作中有字母“t”和公司中有字母“v”的数据。我该怎么做?
我找到了如何做到这一点 here!对于我的架构,我这样写:
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)
现在,使用以下查询,我得到了我想要的结果:
{
jobs(search: "transpo") {
id
company
category
job
createdAt
}
}