Skip to content

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

@leonardoarbache

Description

@leonardoarbache

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions