Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bson.errors.InvalidDocument: cannot encode object: Decimal('3.5'), of type: <class 'decimal.Decimal'> #12

Open
leonardoarbache opened this issue Apr 15, 2021 · 9 comments

Comments

@leonardoarbache
Copy link

I have the follow document structure:

{
 ...
  "resultado": [
    {
      "badges": [
        {
          "codigo": "123",
          "url_imagem": "https://docs.mongoengine.org/apireference.html#mongoengine.fields.URLField",
          "valor": 3,
          "nome": "123",
          "descricao": "123",
          "master": false,
          
    }
   }
  ]
}

I declared the badge document as:

from mongoengine import (
    EmbeddedDocument, fields,
    DynamicDocument
)
class Badge(EmbeddedDocument):
    codigo = fields.StringField(required=False)
    url_imagem = fields.URLField(required=True)
    valor = fields.DecimalField(
        min_value=0,
        max_value=100,
        required=True
    )
    nome = fields.StringField(required=True)
    descricao = fields.StringField(required=True)
    master = fields.BooleanField(required=True)

I made a filter to filter the DecimalField on the EmbeddedDocument Badge:

import django_mongoengine_filter as mongofilter

class RelatorioFilterSet(mongofilter.FilterSet):
        badge_valor = mongofilter.NumberFilter(
        name='resultado__badges__valor'
    )

I'm trying to do the following request:

127.0.0.1:8000/relatorios/?badge_valor=3.5

But i always receive this error:

bson.errors.InvalidDocument: cannot encode object: Decimal('3.5'), of type: <class 'decimal.Decimal'>

I've already tried and observed that:

  • import Decimal128 from bson.decimal128 and save the decimalfield on the document with Decimal128('3.5') did not resolve the problem.
  • On the django shell Relatorio.objects.filter(resultado__badges__valor=3.5) works fine.
  • Somehow when i add some character to the filter param on the request it did not raise an error but return nothing (e.g: 127.0.0.1:8000/relatorios/?badge_valor=3.5g)

I don't know what i missing or if there's something wrong. Please, HELP!

@barseghyanartur
Copy link
Owner

@leonardoarbache:

Could you post here all the software versions/system info?

@leonardoarbache
Copy link
Author

Do you mean my requirements ?

asgiref==3.3.1
beautifulsoup4==4.9.3
celery==5.0.5
certifi==2020.12.5
chardet==4.0.0
cycler==0.10.0
Django==3.1.3
django-filter==2.4.0
django-rest-framework-nested==0.0.1
djangorestframework==3.12.2
djangorestframework-jwt==1.11.0
django-mongoengine-filter==0.3.5
django-rest-framework-mongoengine==3.4.1
django-cors-headers==3.7.0
flake8==3.8.4
gunicorn==20.0.4
html5lib==1.1
idna==2.10
importlib-metadata==2.0.0
kiwisolver==1.3.1
lxml==4.6.2
Markdown==3.3.3
matplotlib==3.3.4
mccabe==0.6.1
mysql==0.0.2
mysqlclient==2.0.3
mongoengine==0.20.0
numpy==1.19.5
pandas==1.1.5
Pillow==8.1.0
psycopg2-binary==2.8.6
pycodestyle==2.6.0
pyflakes==2.2.0
PyJWT==1.7.1
PyMySQL==0.10.1
pyparsing==2.4.7
python-dateutil==2.8.1
python-decouple==3.4
python-pptx==0.6.18
pytz==2020.4
requests==2.25.1
redis==3.5.0
six==1.15.0
soupsieve==2.1
sqlparse==0.4.1
urllib3==1.26.3
webencodings==0.5.1
whitenoise==5.2.0
XlsxWriter==1.3.7
xlwt==1.3.0
zipp==3.4.0

@barseghyanartur
Copy link
Owner

Also version of the mongodb and Python would be handy.

@leonardoarbache
Copy link
Author

@barseghyanartur i'm using python3.8.0 and for mongo i'm using an docker container mongo:latest.

@barseghyanartur
Copy link
Owner

Could you check which "latest" is it?

@leonardoarbache
Copy link
Author

mongo version 4.4.5

@oussjarrousse
Copy link
Contributor

I would like to work on this if it is still relevant. @leonardoarbache Still interested in a solution?

@leonardoarbache
Copy link
Author

Yeah I'm still interested.
If you can help would be awesome, @oussjarrousse :)

@dolho
Copy link

dolho commented May 6, 2023

I had a similar issue. The problem is with mongoengine not being able to handle decimals inside dicts properly, so I had to do following workaround and use float instead of decimal, which was sufficient for my use case:

import django_mongoengine_filter
from django import forms

class FloatFilter(django_mongoengine_filter.NumberFilter):
    field_class = forms.FloatField

And then than use it like this

import django_mongoengine_filter


from .helpers import FloatFilter


class MyFilter(django_mongoengine_filter.FilterSet):
    float_field = FloatFilter(name="float_field")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants