使用DjangoFilterConnectionField时有什么方法可以去除边缘和节点?

问题描述 投票:0回答:1

我开始在django中使用石墨烯,现在我不需要边缘和节点的所有开销,我知道这是用于分页的,但是现在我只需要模型的字段即可。需要明确的是,我仍然希望能够使用filterset我只是不知道如何消除边缘和节点开销。我试图使用graphene.List,但我无法为其添加过滤器集。因此,而不是这样做

{users(nameIcontains:"a")
{
   edges{
     node{
       name
     }
   }
}

我想这样做

{users(nameIcontains:"a")
{
  name
}
django graphene-python graphene-django
1个回答
0
投票
from graphene import ObjectType
from graphene_django import DjangoObjectType

class UserType(DjangoObjectType):
    class Meta:
        filter_fields = {'id': ['exact']}
        model = User    


class Query(ObjectType):
    all_users = List(UserType)

    @staticmethod
    def resolve_all_users(root, info, **kwargs):
        users = User.objects.all()
        # filtering like user.objects.filter ....

        return all_users

[如果您要基于某些条件进行过滤,例如department_id可选的social_club_id

class Query(ObjectType):
    all_users = List(
        UserType,
        department_id=ID(required=True),
        social_club_id=ID(),    # optional
    )

    @staticmethod
    def resolve_all_users(root, info, department_id, **kwargs):
        social_club_id = kwargs.pop('social_club_id', None)

        users = User.objects.all()
        # filtering like user.objects.filter ....

        return all_users.objects.filter(department_id=department_id)

© www.soinside.com 2019 - 2024. All rights reserved.