首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >石墨烯-django将models.BigInteger()转换为graphene.Integer()

石墨烯-django将models.BigInteger()转换为graphene.Integer()
EN

Stack Overflow用户
提问于 2017-08-11 05:56:28
回答 3查看 1.5K关注 0票数 1

我用的是石墨烯-姜戈。

我试图从models.BigInteger()字段中检索数据,但是当我在graphiQL中进行查询时,我得到了以下错误

代码语言:javascript
复制
{
  "errors": [
    {
      "message": "Int cannot represent non 32-bit signed integer value: 2554208328"
    }
  ],
  "data": {
    "match": null
  }
}

有人知道我怎样才能强迫石墨烯给我数据吗?

EN

回答 3

Stack Overflow用户

发布于 2017-08-12 00:48:38

我最终使用了一个自定义标量。如果在graphene-django中自动转换为更好的标量会更好,但下面是我如何解决这个问题的。我用定制的BigInt标量编写了一个converter.py文件,如果我们有一个大于MAX_INT的数字,它将使用浮点型而不是整型

代码语言:javascript
复制
# converter.py
from graphene.types import Scalar
from graphql.language import ast
from graphene.types.scalars import MIN_INT, MAX_INT

class BigInt(Scalar):
    """
    BigInt is an extension of the regular Int field
        that supports Integers bigger than a signed
        32-bit integer.
    """
    @staticmethod
    def big_to_float(value):
        num = int(value)
        if num > MAX_INT or num < MIN_INT:
            return float(int(num))
        return num

    serialize = big_to_float
    parse_value = big_to_float

    @staticmethod
    def parse_literal(node):
        if isinstance(node, ast.IntValue):
            num = int(node.value)
            if num > MAX_INT or num < MIN_INT:
                return float(int(num))
            return num

然后

代码语言:javascript
复制
# schema.py
from .converter import BigInt
class MatchType(DjangoObjectType):
    game_id = graphene.Field(BigInt)
    class Meta:
        model = Match
        interfaces = (graphene.Node, )
        filter_fields = {}
票数 6
EN

Stack Overflow用户

发布于 2018-08-05 21:29:33

您可以注册新的转换器:

代码语言:javascript
复制
    import graphene
    from graphene_django.converter import convert_django_field

    @convert_django_field.register(models.BigIntegerField)
    def convert_bigint_to_float(field, registry=None):
        return graphene.Float(description=field.help_text, required=not field.null)

这就是它在石墨烯中的使用方式-django本身。

票数 3
EN

Stack Overflow用户

发布于 2021-06-29 17:36:42

这是来自石墨烯的源代码(但我猜还没有发布):https://github.com/graphql-python/graphene/blob/485b1ed325287fd721b13aac8b4ec872d6295c6a/graphene/types/scalars.py#L85

代码语言:javascript
复制
class BigInt(Scalar):
"""
The `BigInt` scalar type represents non-fractional whole numeric values.
`BigInt` is not constrained to 32-bit like the `Int` type and thus is a less
compatible type.
"""

@staticmethod
def coerce_int(value):
    try:
        num = int(value)
    except ValueError:
        try:
            num = int(float(value))
        except ValueError:
            return None
    return num

serialize = coerce_int
parse_value = coerce_int

@staticmethod
def parse_literal(ast):
    if isinstance(ast, IntValueNode):
        return int(ast.value)

我认为另一个答案中的代码实际上可能更好,但我想链接这个以防万一

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45624042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档