如何使用过滤器搜索特定的数据集
Posted
技术标签:
【中文标题】如何使用过滤器搜索特定的数据集【英文标题】:How to do search for a specific set of data by using a filter 【发布时间】:2019-12-04 10:34:00 【问题描述】:我有一个标签模型,我想搜索具有特定名称的特定标签。这是我目前所拥有的。
from datetime import datetime
from graphene_sqlalchemy import SQLAlchemyObjectType,SQLAlchemyConnectionField
from database.base import db_session
from database.models.model_post import ModelPost
from database.models.model_image import ModelImage
from database.models.model_posttag import PostsTags
from database.models.model_tag import ModelTag
from database.models.model_user import ModelUser
from database.models.model_locale import ModelLocale
import graphene
from helpers import utils
# Create a generic class to mutualize description of user attributes for both queries and mutations
class TagAttribute:
name = graphene.String(description="Tag Name")
isactive = graphene.Boolean(description="is active")
class Tag(SQLAlchemyObjectType):
"""Tag node."""
class Meta:
model = ModelTag
interfaces = (graphene.relay.Node,)
class Query(graphene.ObjectType):
tagToFind = graphene.Field(lambda: Tag, name=graphene.String())
def resolve_find_tag(self, info, **kwargs):
query = Tag.get_query(info)
name = kwargs.get("name")
return query.filter(TagModel.name == name).first()
class CreateTagInput(graphene.InputObjectType, TagAttribute):
"""Arguments to create a tag."""
pass
class CreateTag(graphene.Mutation):
"""Mutation to create a tag."""
tag = graphene.Field(lambda: Tag, description="tag created by this mutation.")
class Arguments:
input = CreateTagInput(required=True)
def mutate(self, info, input):
data = utils.input_to_dictionary(input)
data['created_at'] = datetime.utcnow()
data['updated_at'] = datetime.utcnow()
tag = ModelTag(**data)
db_session.add(tag)
db_session.commit()
return CreateTag(tag=tag)
class UpdateTagInput(graphene.InputObjectType, TagAttribute):
"""Arguments to update a tag."""
id = graphene.ID(required=True, description="Global Id of the tag.")
class UpdateTag(graphene.Mutation):
"""Update a tag."""
tag = graphene.Field(lambda: Tag, description="tag updated by this mutation.")
class Arguments:
input = UpdateTagInput(required=True)
def mutate(self, info, input):
data = utils.input_to_dictionary(input)
data['updated_at'] = datetime.utcnow()
tag = db_session.query(ModelTag).filter_by(id=data['id'])
tag.update(data)
db_session.commit()
tag = db_session.query(ModelTag).filter_by(id=data['id']).first()
return UpdateTag(tag=tag)
这是我的一个查询
query FindTagByName($name: String ="searchstring")
findTag(name: $name)
id
name
但我收到此错误
你能告诉我我在这里做错了什么吗? 谢谢
【问题讨论】:
【参考方案1】:你的 Query
有 tagToFind
文件(应该是蛇形的),但你解决了 find_tag
所以石墨烯找不到它。
将tagToFind
更改为find_tag
以修复它。
【讨论】:
以上是关于如何使用过滤器搜索特定的数据集的主要内容,如果未能解决你的问题,请参考以下文章