【问题标题】:Is there any way to remove edges and node when using DjangoFilterConnectionField?使用 DjangoFilterConnectionField 时有什么方法可以删除边和节点?
【发布时间】:2020-06-03 08:09:54
【问题描述】:

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

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

我想这样做

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

【问题讨论】:

    标签: django graphene-python graphene-django


    【解决方案1】:
    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)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-14
      • 2014-07-01
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多