mongoengine: Dict field can't handle python's Decimal
Attempt to save decimal.Decimal
object as one of the values in DictField
raises bson.errors.InvalidDocument: Cannot encode object: Decimal('20000')
About this issue
- Original URL
- State: open
- Created 10 years ago
- Comments: 15 (12 by maintainers)
For anyone looking for a quick answer:
You need to convert the
Decimal
to BSON’sDecimal128
before inserting to the database. More info:https://api.mongodb.com/python/current/api/bson/decimal128.html#module-bson.decimal128
You also require MongoDB 3.4+.
Because as a framework we should be able to deal with Python data types that are being saved to mongo.
It’s not a duplicate.
DictField
should handle pythonDecimal
’s somehow as they are native Python objects. Arguing aboutDecimalField
is an offtopic remark. I’m no longer interested in MongoEngine and Mongo (and Python)… just my final note here.