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.

Django integer fields gets transformed to Float?

See original GitHub issue

On a project of mine I noticed this unexpected behavior:

Using graphene_django with graphene.relay converts IntegrierFields of my models to graphene.Float properties in the API, e.g.:

class Incident(models.Model):
    incident_id = models.IntegerField(unique=True)

class IncidentType(graphene_django.types.DjangoObjectType):
    class Meta:
        model = Incident
        interfaces = (graphene.relay.Node,)
        filter_fields = {
            'incident_id': ['exact'],
    }

class Query:
    incident = graphene.relay.Node.Field(IncidentType)
    incidents = DjangoFilterConnectionField(IncidentType)

To be precise, using the incident attribute shows the incidentId to be of type Int. But using the incidents attribute suggests that incidentId is of type Float.

I’d like to point out, that there is no error occurring, but it strikes me as weird.

I observed this with:

  • Django 2.2
  • django-filter 2.1.0
  • graphene 2.1.3
  • graphene-django 2.2.0
  • graphql-core 2.1
  • graphql-relay 0.4.5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
djmazecommented, Jul 1, 2021

Same problem as @Gamsake here, with 2.15.0. The problem seems to occur when using filter_fields on a django model’s id column.

Our workaround is to define and set an explicit filterset class:

class Country(models.Model):
    name = models.CharField(max_length=255, unique=True)


class CountryFilter(django_filters.FilterSet):
    id = django_filters.ModelChoiceFilter(queryset=Country.objects.all().values_list("id", flat=True))

    class Meta:
        model = Country
        fields = {
            "id": ["exact"],
            "name": ["exact"],
        }


class CountryType(DjangoObjectType):
    # ...

    class Meta:
        model = Country
        # filter_fields = ["id", "name"]
        filterset_class = CountryFilter
2reactions
Gamsakecommented, Aug 9, 2019

@jkimbo Yes. I’m using 2.4.0 the latest now and ‘id’ the field of my model has Int type, but it is converted into Float type. Exactly same issue as above first post. I’ve done triple checks but i never solve this problem. any clue plz.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I get Django to display integers instead of floats
Main problem is to know when a float is meant to be an integer (instead of being only near an integer value). If...
Read more >
Model field reference - Django documentation
An IntegerField that automatically increments according to available IDs. You usually won't need to use this directly; a primary key field will ...
Read more >
6 Ways to Convert String to Float in Python - FavTutor
You can use the float() function to convert any data type into a floating-point number. This method only accepts one parameter. If you...
Read more >
Numeric field types - OpenSearch documentation
A scaled float field type is a floating-point value that is multiplied by the scale factor and stored as a long value. It...
Read more >
Model Field - Django ORM Working - Part 2
For example, to_python for IntegerField will convert the value to Python integer. The original value could be string or float . Every field...
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