【问题标题】:How to use OR condition for icontains in multiple fields with graphene-django如何使用石墨烯-django 对多个字段中的图标使用 OR 条件
【发布时间】:2021-08-11 02:22:18
【问题描述】:

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

应返回工作中包含字母“t”或公司中包含字母“v”的数据,而不是工作中包含字母“t”和公司中包含字母“v”的数据。我该怎么做?

【问题讨论】:

    标签: python django graphql graphene-python graphene-django


    【解决方案1】:

    我发现了如何做到这一点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
      }
    }
    

    【讨论】:

    • 是的,这行得通,但请在您的字段中添加indexes,否则当您的网站获得一些实际数据量时,它会慢慢爬起来。
    • 谢谢@Melvyn,我认为它是由 Django 自动设置的。我在我的数据库中看到了每条记录的 ID。应该很酷吧?
    猜你喜欢
    • 2019-12-20
    • 2019-08-19
    • 2019-04-03
    • 2018-12-06
    • 2020-08-11
    • 2021-01-19
    • 2019-03-31
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多