返回石墨烯中的可迭代查询集

发布于 2025-01-17 12:07:08 字数 947 浏览 0 评论 0原文

在我的解析器中,我尝试使用通用的车辆ID在行上汇总距离,迫使我在查询上调用value('warter_id') ,从而创建了一个可抛出的QuerySet,以抛出:
例外:接收到不兼容的实例“ {'warter_id':x,'sum_distance':y}”
返回GraphQL时。

class AssignmentType(DjangoObjectType):
    class Meta:
        model = Assignment

    sum_distance = graphene.Int()

    def resolve_sum_distance(self, info):
        return getattr(self, "sum_distance", None)

class Query(graphene.ObjectType):
    all_assignments = graphene.List(AssignmentType)

    def resolve_all_assignments(self, info, **kwargs):
        return Assignment.objects.order_by().values("vehicle_id").\
            annotate(sum_distance=Sum("distance", output_field=IntegerField()))
       

schema = graphene.Schema(query=Query)

Django 4.0.3
Graphene 2.19

从我在类似帖子中看到的答案中,例如这不应该失败吗?

In my resolver, I attempt to aggregate distance driven on rows with a common vehicle id, forcing me to call values('vehicle_id') on the query and thus creating a iterable queryset which throws:

Exception: Received incompatible instance "{'vehicle_id': x, 'sum_distance': y}"

when returned to graphql.

class AssignmentType(DjangoObjectType):
    class Meta:
        model = Assignment

    sum_distance = graphene.Int()

    def resolve_sum_distance(self, info):
        return getattr(self, "sum_distance", None)

class Query(graphene.ObjectType):
    all_assignments = graphene.List(AssignmentType)

    def resolve_all_assignments(self, info, **kwargs):
        return Assignment.objects.order_by().values("vehicle_id").\
            annotate(sum_distance=Sum("distance", output_field=IntegerField()))
       

schema = graphene.Schema(query=Query)

django 4.0.3

graphene 2.19

From the answers I have seen in similar posts like https://stackoverflow.com/a/51425324/7267684, this should not be failing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2025-01-24 12:07:08

尝试一次不确定

from django.db.models import IntegerField, Sum, Value
from django.db.models.functions import Coalesce
    
    
class AssignmentType(DjangoObjectType):
    class Meta:
        model = Assignment

    sum_distance = graphene.Int()

    def resolve_sum_distance(self, info):
        return getattr(self, "sum_distance", 0)

class Query(graphene.ObjectType):
    all_assignments = graphene.List(AssignmentType)

    def resolve_all_assignments(self, info, **kwargs):
        return  Assignment.objects.order_by().values("vehicle_id")annotate(sum_distance=Coalesce(Sum("distance", output_field=IntegerField()), Value(0)))

Try This Once Not Sure

from django.db.models import IntegerField, Sum, Value
from django.db.models.functions import Coalesce
    
    
class AssignmentType(DjangoObjectType):
    class Meta:
        model = Assignment

    sum_distance = graphene.Int()

    def resolve_sum_distance(self, info):
        return getattr(self, "sum_distance", 0)

class Query(graphene.ObjectType):
    all_assignments = graphene.List(AssignmentType)

    def resolve_all_assignments(self, info, **kwargs):
        return  Assignment.objects.order_by().values("vehicle_id")annotate(sum_distance=Coalesce(Sum("distance", output_field=IntegerField()), Value(0)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文