question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert DecimalField to String rather than Float

See original GitHub issue

DecimalField entries are currently being converted to Float (https://github.com/graphql-python/graphene-django/blob/8f5a425859ea380d0640c441c5c7f3b1acae17b4/graphene_django/form_converter.py#L56-L59)

Full support for Decimal would be great, but in the meantime I propose that DecimalField should be converted to String instead. Once you’ve converted a Decimal to a Float, you’ve probably lost the benefit of why you used Decimal in the first place, since you’ll now have values like 3.14000000000000012, rather than 3.14. If it is converted to String, it is simple enough for the user to convert that String back to Decimal, whereas converting from Float to Decimal is not straightforward.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:17
  • Comments:17

github_iconTop GitHub Comments

8reactions
picturedotscommented, Jun 1, 2017

As a workaround for this particular problem, I created a Decimal type, and then am explicitly setting that field in the DjangoObjectType class. For example, I created a Decimal type like this

import decimal

from graphene.types import Scalar
from graphql.language import ast

class Decimal(Scalar):
    """ 
    The `Decimal` scalar type represents a python Decimal.
    """
    @staticmethod
    def serialize(dec):
        assert isinstance(dec, decimal.Decimal), (
            'Received not compatible Decimal "{}"'.format(repr(dec))
        )
        return str(dec)
    
    @staticmethod
    def parse_value(value):
        return decimal.Decimal(value)
    
    @classmethod
    def parse_literal(cls, node):
        if isinstance(node, ast.StringValue):
            return cls.parse_value(node.value)

and then explicitly replace the field like this:

class MyModelType(DjangoObjectType):
    my_decimal_field = Decimal()
    class Meta:
        model = MyModel

assuming that I have a Django model MyModel with Decimal field my_decimal_field.

5reactions
i6mi6commented, Jul 9, 2021

Has this been documented in the changelog? It broke our existing app logic because now suddenly we started getting strings rather than floats. Is there a way to default to previous behavior?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Django DecimalField let me store Float or string?
A Decimal is more similar to an int than to a float. It's a fixed point integer, which is basically an integer with...
Read more >
Decimal field conversions - InfoSphere DataStage - IBM
Decimal field conversions. By default InfoSphere® DataStage® converts decimal fields to and from all numeric data types and to and from string fields....
Read more >
#8233 (Decimal Field can accept float value) – Django
The float value can be converted using the DecimalField format_number method, ... try: # if the value is a float, convert it to...
Read more >
6 Ways to Convert String to Float in Python - FavTutor
Learn how to convert string to float in python in 6 different ways. ... deal with characters but instead, it works only with...
Read more >
The Type Hierarchy - SQLAlchemy 1.4 Documentation
Default scale to use when converting from floats to Python decimals. ... and should instead implement TypeDecorator.process_bind_param() so ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found