Skip to content
This repository was archived by the owner on Jan 12, 2026. It is now read-only.

feat(fields): add DecimalField support for MongoDB backend - #17

Merged
armonge merged 3 commits into
mainfrom
chore/decimal-field
Jul 7, 2025
Merged

feat(fields): add DecimalField support for MongoDB backend#17
armonge merged 3 commits into
mainfrom
chore/decimal-field

Conversation

@armonge

@armonge armonge commented May 28, 2025

Copy link
Copy Markdown
Contributor
  • 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
armonge marked this pull request as ready for review May 28, 2025 20:09
@armonge
armonge requested a review from gersmann July 7, 2025 07:36
@gersmann

gersmann commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

@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?

Comment thread django_mongodb/operations.py Outdated
@armonge

armonge commented Jul 7, 2025

Copy link
Copy Markdown
Contributor Author

@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
https://www.mongodb.com/docs/manual/tutorial/model-monetary-data/#std-label-numeric-decimal

@gersmann

gersmann commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

@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 https://www.mongodb.com/docs/manual/tutorial/model-monetary-data/#std-label-numeric-decimal

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.
@armonge
armonge requested a review from gersmann July 7, 2025 09:28
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+.
@armonge
armonge merged commit b28ee3d into main Jul 7, 2025
2 checks passed
@armonge
armonge deleted the chore/decimal-field branch July 7, 2025 12:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants