feat(fields): add DecimalField support for MongoDB backend#17
Conversation
armonge
commented
May 28, 2025
- Implement adapt_decimalfield_value method in DatabaseOperations
- Add DecimalFieldModel for testing decimal field functionality
- Include test case to verify DecimalField creation and storage
- Implement adapt_decimalfield_value method in DatabaseOperations - Add DecimalFieldModel for testing decimal field functionality - Include test case to verify DecimalField creation and storage
|
@armonge hm damn. In this PR - are we converting Decimal to strings? On the backend side I did define this as number :( Actually MongoDB has native decimal support, should we use the native decimal in both places? |
I'd say yes, I can work on making it so. Seems like the pieces to put something like this in place are available https://pymongo.readthedocs.io/en/stable/api/bson/decimal128.html |
WDYT - cleaner solution? |
Add conversion logic for Django DecimalField to use BSON Decimal128 for storage and retrieval in MongoDB. Implement database converters and adapters for DecimalField, ensuring compatibility with both Decimal128 and legacy string formats. Update query logic to ensure correct value conversion for __in lookups. Add comprehensive tests for DecimalField CRUD, filtering, ordering, aggregation, and backward compatibility.
Introduce a custom DecimalField in `django_mongodb.fields` to ensure proper conversion of Python Decimal to BSON Decimal128 for MongoDB operations on Django versions prior to 5.2. Update documentation and test coverage to clarify usage and compatibility. Refactor models and exports to support the new field, and recommend its use for Django <5.2 while allowing the built-in field for Django 5.2+.